From 10cee781a790ae368ae5502c63038eb8a4e99c6c Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 22 Sep 2022 08:00:41 +1000 Subject: [PATCH] Add `-ci` flag to wails init --- .github/workflows/release.yml | 5 +- .../commands/initialise/initialise.go | 65 +++++++++++++++++-- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 34ca00fa6..c2644d0ee 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -66,10 +66,7 @@ jobs: run: | mkdir -p ./test-${{ matrix.template }} cd ./test-${{ matrix.template }} - wails init -n ${{ matrix.template }} -t ${{ matrix.template }} -m + wails init -n ${{ matrix.template }} -t ${{ matrix.template }} -ci cd ${{ matrix.template }} cat go.mod - sed -i -e 's/\/\/ replace/replace/g' go.mod - sed -i -e "s/=>.*/=> ${GITHUB_WORKSPACE}\/v2/g" go.mod - cat go.mod wails build -v 2 diff --git a/v2/cmd/wails/internal/commands/initialise/initialise.go b/v2/cmd/wails/internal/commands/initialise/initialise.go index 43863c1b0..1fe7274bf 100644 --- a/v2/cmd/wails/internal/commands/initialise/initialise.go +++ b/v2/cmd/wails/internal/commands/initialise/initialise.go @@ -1,8 +1,10 @@ package initialise import ( + "bufio" "fmt" "io" + "log" "os" "os/exec" "path/filepath" @@ -36,9 +38,9 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { projectName := "" command.StringFlag("n", "Name of project", &projectName) - // Skip mod tidy - skipModTidy := false - command.BoolFlag("m", "Skip mod tidy", &skipModTidy).Hidden() + // For CI + ciMode := false + command.BoolFlag("ci", "CI Mode", &ciMode).Hidden() // Setup project directory projectDirectory := "" @@ -135,14 +137,14 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { // Try to discover author details from git config findAuthorDetails(options) - return initProject(options, quiet, skipModTidy) + return initProject(options, quiet, ciMode) }) return nil } // initProject is our main init command -func initProject(options *templates.Options, quiet bool, skipModTidy bool) error { +func initProject(options *templates.Options, quiet bool, ciMode bool) error { // Start Time start := time.Now() @@ -159,7 +161,12 @@ func initProject(options *templates.Options, quiet bool, skipModTidy bool) error return err } - if !skipModTidy { + err = os.Chdir(options.TargetDir) + if err != nil { + return err + } + + if !ciMode { // Run `go mod tidy` to ensure `go.sum` is up to date cmd := exec.Command("go", "mod", "tidy") cmd.Dir = options.TargetDir @@ -172,6 +179,14 @@ func initProject(options *templates.Options, quiet bool, skipModTidy bool) error if err != nil { return err } + } else { + // Update go mod + workspace := os.Getenv("GITHUB_WORKSPACE") + println("GitHub workspace:", workspace) + if workspace == "" { + os.Exit(1) + } + updateReplaceLine(workspace) } if options.InitGit { @@ -249,3 +264,41 @@ func findAuthorDetails(options *templates.Options) { } } } + +func updateReplaceLine(targetPath string) { + file, err := os.Open("go.mod") + if err != nil { + log.Fatal(err) + } + + var lines []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + err = file.Close() + if err != nil { + log.Fatal(err) + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } + + for i, line := range lines { + println(line) + if strings.HasPrefix(line, "// replace") { + println("Found replace line") + splitLine := strings.Split(line, " ") + splitLine[5] = targetPath + lines[i] = strings.Join(splitLine[1:], " ") + continue + } + } + + err = os.WriteFile("go.mod", []byte(strings.Join(lines, "\n")), 0644) + if err != nil { + log.Fatal(err) + } +}