summaryrefslogtreecommitdiff
path: root/doPublish.go
blob: 4a3e9a3386c60a18dfb53552ec69d39a8db3f895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"go.wit.com/lib/env"
	"go.wit.com/lib/protobuf/argvpb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func rillRestore(repo *gitpb.Repo) error {
	if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
		return nil
	}
	if me.forge.Config.IsPrivate(repo.GetGoPath()) {
		return nil
	}

	cmd := []string{"go-mod-clean", "lax"}
	if env.True("--strict") {
		cmd = []string{"go-mod-clean", "purge"}
		repo.RunQuiet(cmd)
		cmd = []string{"go-mod-clean", "--restore"}
	}

	var err error
	if env.Verbose() {
		_, err = repo.RunQuiet(cmd)
	} else {
		_, err = repo.RunQuiet(cmd)
	}
	if err != nil {
		errs := fmt.Sprintf("%v failed (%v)", cmd, err)
		return errors.New(errs)
	}
	return nil
}

func doPublish() error {
	initForge()
	env.PrintTable()

	log.Printf("Running go-mod-clean on (%d) repos. (1 second per hundred)\n", me.forge.Repos.Len())
	restored := me.forge.RunOnReposNew(me.forge.Repos, rillRestore)
	restored = restored.SortActual()
	footer := restored.PrintPublishNewTB()
	fmt.Printf("restored.len(%d) with errors: %s\n", restored.Len(), footer)

	// publish := me.forge.RunOnReposNewDumb(me.forge.Repos, rePrepareReleaseNew) // doesn't use Rill()
	publish := me.forge.RunOnReposNew(me.forge.Repos, rePrepareReleaseNew)
	if publish == nil {
		return errors.New("nothing to publish. you actually have to write code first")
	}
	for repo := range publish.IterAll() {
		err := me.forge.CheckUpdatingGoDeps(repo.GoDeps, publish)
		if err != nil {
			repo.State = fmt.Sprintf("%s err (%v)\n", repo.Namespace, err)
		} else {
			cmd := []string{"go-mod-clean", "strict"}
			repo.RunRealtime(cmd)
		}
	}

	for repo := range publish.IterAll() {
		// update the target version
		// repo.IncrementTargetMinor()
		repo.IncrementTargetRevision()
	}

	publish = publish.SortActual()
	footer = publish.PrintPublishNewTB()
	fmt.Printf("publish.len(%d) PrintPublishTable() footer: %s\n", publish.Len(), footer)

	tryme := findNext(publish)
	if len(tryme) == 0 {
		return errors.New("can't find something safe to publish")
	}

	startRepo := me.forge.Repos.FindByNamespace("go.wit.com/lib/xgb")
	if startRepo == nil {
		return errors.New("a startRepo can't be found")
	}

	releaseReason := "new attempt"

	for _, repo := range tryme {
		log.Printf("tryme: %s reason(%s)", repo.FullPath, repo.State)
		gomod, err := os.ReadFile(filepath.Join(repo.FullPath, "go.mod"))
		if err != nil {
			panic("go.mod missing")
		}
		log.Info(string(gomod))
		if env.True("--doit") {
			err := doRelease(repo, startRepo, releaseReason)
			if err != nil {
				return err
			}
		} else {
			log.Printf("WOULD HAVE RUN: doRelease(%s, %s, %s) %s\n", repo.Namespace, startRepo.Namespace, releaseReason, repo.GetTargetVersion())
		}
	}

	if !env.True("--doit") {
		return errors.New("run with --doit to actually try to publish")
	}
	// me.forge.RillFuncError(rillPurge)

	/*
		saferepo := me.forge.Repos.FindByNamespace("go.wit.com/lib/xgb") // safe to run things here
		if saferepo == nil {
			return log.Errorf("need a safe place to run GO commands from")
		}
		os.Chdir(saferepo.FullPath)

		if os.Getenv("GUIRELEASE_REASON") == "" {
			log.Info("$ENV[GUIRELEASE_REASON] was not set")
			reason := fhelp.InputFromUser("set tag message:")
			if reason == "" {
				argvpb.BadExit("merge failed", fmt.Errorf("GUIRELEASE_REASON was blank"))
			}
			os.Setenv("GUIRELEASE_REASON", reason)
		}

		var cmd []string

		cmd = []string{"forge", "merge", "--all"}
		if _, err := shell.RunRealtimeError(cmd); err != nil {
			argvpb.BadExit("merge failed", nil)
		}

		cmd = []string{"forge", "merge", "check"}
		if _, err := shell.RunRealtimeError(cmd); err != nil {
			if _, err := shell.RunVerbose(cmd); err != nil {
				argvpb.BadExit("merge failed", nil)
			}
		}
		// todo: os.Stat() file
		time.Sleep(time.Second)
		me.forge.Repos.Load()

		if err := doInstall(me.forge.Repos); err != nil {
			log.Info("doInstall() failed", err)
			argvpb.BadExit("merge failed", nil)
		}

		if os.Getenv("GUIRELEASE_REASON") == "" {
			os.Setenv("GUIRELEASE_REASON", "automated")
		}
	*/

	argvpb.GoodExit("PUBLISH WORKED")
	return nil
}