From de229a1e5a5cc260da6b635ae6f7aa8d72b13fdc Mon Sep 17 00:00:00 2001 From: David Gamba Date: Thu, 13 Jun 2024 20:54:21 -0600 Subject: [PATCH] bake: initial release --- bake/LICENSE | 363 ++++++++++++++++++++++++++++ bake/README.adoc | 142 +++++++++++ bake/ast.go | 126 ++++++++++ bake/ast_getoptions.go | 135 +++++++++++ bake/ast_handle_options.go | 228 +++++++++++++++++ bake/build.env | 2 + bake/build.go | 138 +++++++++++ bake/examples/website/.gitignore | 2 + bake/examples/website/README.adoc | 37 +++ bake/examples/website/bake/go.mod | 10 + bake/examples/website/bake/go.sum | 8 + bake/examples/website/bake/main.go | 233 ++++++++++++++++++ bake/examples/website/diagram.dot | 4 + bake/examples/website/go.mod | 3 + bake/examples/website/index-en.adoc | 6 + bake/examples/website/index-es.adoc | 6 + bake/examples/website/main.go | 36 +++ bake/go.mod | 17 ++ bake/go.sum | 20 ++ bake/init.go | 78 ++++++ bake/main.go | 166 +++++++++++++ bake/templates/main.go.gotmpl | 68 ++++++ bake/tree.go | 174 +++++++++++++ bake/utils.go | 25 ++ 24 files changed, 2027 insertions(+) create mode 100644 bake/LICENSE create mode 100644 bake/README.adoc create mode 100644 bake/ast.go create mode 100644 bake/ast_getoptions.go create mode 100644 bake/ast_handle_options.go create mode 100644 bake/build.env create mode 100644 bake/build.go create mode 100644 bake/examples/website/.gitignore create mode 100644 bake/examples/website/README.adoc create mode 100644 bake/examples/website/bake/go.mod create mode 100644 bake/examples/website/bake/go.sum create mode 100644 bake/examples/website/bake/main.go create mode 100644 bake/examples/website/diagram.dot create mode 100644 bake/examples/website/go.mod create mode 100644 bake/examples/website/index-en.adoc create mode 100644 bake/examples/website/index-es.adoc create mode 100644 bake/examples/website/main.go create mode 100644 bake/go.mod create mode 100644 bake/go.sum create mode 100644 bake/init.go create mode 100644 bake/main.go create mode 100644 bake/templates/main.go.gotmpl create mode 100644 bake/tree.go create mode 100644 bake/utils.go diff --git a/bake/LICENSE b/bake/LICENSE new file mode 100644 index 0000000..e87a115 --- /dev/null +++ b/bake/LICENSE @@ -0,0 +1,363 @@ +Mozilla Public License, version 2.0 + +1. Definitions + +1.1. "Contributor" + + means each individual or legal entity that creates, contributes to the + creation of, or owns Covered Software. + +1.2. "Contributor Version" + + means the combination of the Contributions of others (if any) used by a + Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + + means Source Code Form to which the initial Contributor has attached the + notice in Exhibit A, the Executable Form of such Source Code Form, and + Modifications of such Source Code Form, in each case including portions + thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + a. that the initial Contributor has attached the notice described in + Exhibit B to the Covered Software; or + + b. that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the terms of + a Secondary License. + +1.6. "Executable Form" + + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + + means a work that combines Covered Software with other material, in a + separate file or files, that is not Covered Software. + +1.8. "License" + + means this document. + +1.9. "Licensable" + + means having the right to grant, to the maximum extent possible, whether + at the time of the initial grant or subsequently, any and all of the + rights conveyed by this License. + +1.10. "Modifications" + + means any of the following: + + a. any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered Software; or + + b. any new file in Source Code Form that contains any Covered Software. + +1.11. "Patent Claims" of a Contributor + + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the License, + by the making, using, selling, offering for sale, having made, import, + or transfer of either its Contributions or its Contributor Version. + +1.12. "Secondary License" + + means either the GNU General Public License, Version 2.0, the GNU Lesser + General Public License, Version 2.1, the GNU Affero General Public + License, Version 3.0, or any later versions of those licenses. + +1.13. "Source Code Form" + + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that controls, is + controlled by, or is under common control with You. For purposes of this + definition, "control" means (a) the power, direct or indirect, to cause + the direction or management of such entity, whether by contract or + otherwise, or (b) ownership of more than fifty percent (50%) of the + outstanding shares or beneficial ownership of such entity. + + +2. License Grants and Conditions + +2.1. Grants + + Each Contributor hereby grants You a world-wide, royalty-free, + non-exclusive license: + + a. under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + + b. under Patent Claims of such Contributor to make, use, sell, offer for + sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + + The licenses granted in Section 2.1 with respect to any Contribution + become effective for each Contribution on the date the Contributor first + distributes such Contribution. + +2.3. Limitations on Grant Scope + + The licenses granted in this Section 2 are the only rights granted under + this License. No additional rights or licenses will be implied from the + distribution or licensing of Covered Software under this License. + Notwithstanding Section 2.1(b) above, no patent license is granted by a + Contributor: + + a. for any code that a Contributor has removed from Covered Software; or + + b. for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + + c. under Patent Claims infringed by Covered Software in the absence of + its Contributions. + + This License does not grant any rights in the trademarks, service marks, + or logos of any Contributor (except as may be necessary to comply with + the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + + No Contributor makes additional grants as a result of Your choice to + distribute the Covered Software under a subsequent version of this + License (see Section 10.2) or under the terms of a Secondary License (if + permitted under the terms of Section 3.3). + +2.5. Representation + + Each Contributor represents that the Contributor believes its + Contributions are its original creation(s) or it has sufficient rights to + grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + + This License is not intended to limit any rights You have under + applicable copyright doctrines of fair use, fair dealing, or other + equivalents. + +2.7. Conditions + + Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in + Section 2.1. + + +3. Responsibilities + +3.1. Distribution of Source Form + + All distribution of Covered Software in Source Code Form, including any + Modifications that You create or to which You contribute, must be under + the terms of this License. You must inform recipients that the Source + Code Form of the Covered Software is governed by the terms of this + License, and how they can obtain a copy of this License. You may not + attempt to alter or restrict the recipients' rights in the Source Code + Form. + +3.2. Distribution of Executable Form + + If You distribute Covered Software in Executable Form then: + + a. such Covered Software must also be made available in Source Code Form, + as described in Section 3.1, and You must inform recipients of the + Executable Form how they can obtain a copy of such Source Code Form by + reasonable means in a timely manner, at a charge no more than the cost + of distribution to the recipient; and + + b. You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter the + recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + + You may create and distribute a Larger Work under terms of Your choice, + provided that You also comply with the requirements of this License for + the Covered Software. If the Larger Work is a combination of Covered + Software with a work governed by one or more Secondary Licenses, and the + Covered Software is not Incompatible With Secondary Licenses, this + License permits You to additionally distribute such Covered Software + under the terms of such Secondary License(s), so that the recipient of + the Larger Work may, at their option, further distribute the Covered + Software under the terms of either this License or such Secondary + License(s). + +3.4. Notices + + You may not remove or alter the substance of any license notices + (including copyright notices, patent notices, disclaimers of warranty, or + limitations of liability) contained within the Source Code Form of the + Covered Software, except that You may alter any license notices to the + extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + + You may choose to offer, and to charge a fee for, warranty, support, + indemnity or liability obligations to one or more recipients of Covered + Software. However, You may do so only on Your own behalf, and not on + behalf of any Contributor. You must make it absolutely clear that any + such warranty, support, indemnity, or liability obligation is offered by + You alone, and You hereby agree to indemnify every Contributor for any + liability incurred by such Contributor as a result of warranty, support, + indemnity or liability terms You offer. You may include additional + disclaimers of warranty and limitations of liability specific to any + jurisdiction. + +4. Inability to Comply Due to Statute or Regulation + + If it is impossible for You to comply with any of the terms of this License + with respect to some or all of the Covered Software due to statute, + judicial order, or regulation then You must: (a) comply with the terms of + this License to the maximum extent possible; and (b) describe the + limitations and the code they affect. Such description must be placed in a + text file included with all distributions of the Covered Software under + this License. Except to the extent prohibited by statute or regulation, + such description must be sufficiently detailed for a recipient of ordinary + skill to be able to understand it. + +5. Termination + +5.1. The rights granted under this License will terminate automatically if You + fail to comply with any of its terms. However, if You become compliant, + then the rights granted under this License from a particular Contributor + are reinstated (a) provisionally, unless and until such Contributor + explicitly and finally terminates Your grants, and (b) on an ongoing + basis, if such Contributor fails to notify You of the non-compliance by + some reasonable means prior to 60 days after You have come back into + compliance. Moreover, Your grants from a particular Contributor are + reinstated on an ongoing basis if such Contributor notifies You of the + non-compliance by some reasonable means, this is the first time You have + received notice of non-compliance with this License from such + Contributor, and You become compliant prior to 30 days after Your receipt + of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent + infringement claim (excluding declaratory judgment actions, + counter-claims, and cross-claims) alleging that a Contributor Version + directly or indirectly infringes any patent, then the rights granted to + You by any and all Contributors for the Covered Software under Section + 2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user + license agreements (excluding distributors and resellers) which have been + validly granted by You or Your distributors under this License prior to + termination shall survive termination. + +6. Disclaimer of Warranty + + Covered Software is provided under this License on an "as is" basis, + without warranty of any kind, either expressed, implied, or statutory, + including, without limitation, warranties that the Covered Software is free + of defects, merchantable, fit for a particular purpose or non-infringing. + The entire risk as to the quality and performance of the Covered Software + is with You. Should any Covered Software prove defective in any respect, + You (not any Contributor) assume the cost of any necessary servicing, + repair, or correction. This disclaimer of warranty constitutes an essential + part of this License. No use of any Covered Software is authorized under + this License except under this disclaimer. + +7. Limitation of Liability + + Under no circumstances and under no legal theory, whether tort (including + negligence), contract, or otherwise, shall any Contributor, or anyone who + distributes Covered Software as permitted above, be liable to You for any + direct, indirect, special, incidental, or consequential damages of any + character including, without limitation, damages for lost profits, loss of + goodwill, work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses, even if such party shall have been + informed of the possibility of such damages. This limitation of liability + shall not apply to liability for death or personal injury resulting from + such party's negligence to the extent applicable law prohibits such + limitation. Some jurisdictions do not allow the exclusion or limitation of + incidental or consequential damages, so this exclusion and limitation may + not apply to You. + +8. Litigation + + Any litigation relating to this License may be brought only in the courts + of a jurisdiction where the defendant maintains its principal place of + business and such litigation shall be governed by laws of that + jurisdiction, without reference to its conflict-of-law provisions. Nothing + in this Section shall prevent a party's ability to bring cross-claims or + counter-claims. + +9. Miscellaneous + + This License represents the complete agreement concerning the subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. Any law or regulation which provides that + the language of a contract shall be construed against the drafter shall not + be used to construe this License against a Contributor. + + +10. Versions of the License + +10.1. New Versions + + Mozilla Foundation is the license steward. Except as provided in Section + 10.3, no one other than the license steward has the right to modify or + publish new versions of this License. Each version will be given a + distinguishing version number. + +10.2. Effect of New Versions + + You may distribute the Covered Software under the terms of the version + of the License under which You originally received the Covered Software, + or under the terms of any subsequent version published by the license + steward. + +10.3. Modified Versions + + If you create software not governed by this License, and you want to + create a new license for such software, you may create and use a + modified version of this License if you rename the license and remove + any references to the name of the license steward (except to note that + such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary + Licenses If You choose to distribute Source Code Form that is + Incompatible With Secondary Licenses under the terms of this version of + the License, the notice described in Exhibit B of this License must be + attached. + +Exhibit A - Source Code Form License Notice + + This Source Code Form is subject to the + terms of the Mozilla Public License, v. + 2.0. If a copy of the MPL was not + distributed with this file, You can + obtain one at + http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular file, +then You may include the notice in a location (such as a LICENSE file in a +relevant directory) where a recipient would be likely to look for such a +notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice + + This Source Code Form is "Incompatible + With Secondary Licenses", as defined by + the Mozilla Public License, v. 2.0. + diff --git a/bake/README.adoc b/bake/README.adoc new file mode 100644 index 0000000..2deeded --- /dev/null +++ b/bake/README.adoc @@ -0,0 +1,142 @@ += Bake + +Go Build + Something like Make = Bake ¯\_(ツ)_/¯ 🤷 + +Bake is a Make like tool that allows you to define and run tasks defined in Go code under a `bake/` dir. + +* Tasks have a fully featured argument parser. +* It allows you to see the description of the tasks and subtasks and autocomplete them and their options. +* It also bundles a DAG runner that allows you to make tasks depend on other tasks. + +== Getting Started + +In any directory, run: `bake _bake init` to create a `bake/` directory with an empty project. + +Then run: `bake` to see the available tasks. + +== Install + +* Install using go: ++ +Install the binary into your `~/go/bin`: ++ +---- +go install github.com/DavidGamba/dgtools/bake@latest +---- ++ +Then setup the completion. ++ +For bash: ++ +---- +complete -o default -C bake bake +---- ++ +For zsh: ++ +[source, zsh] +---- +export ZSHELL="true" +autoload -U +X compinit && compinit +autoload -U +X bashcompinit && bashcompinit +complete -o default -C bake bake +---- + +== Example Task + +NOTE: A more in depth example can be found https://github.com/DavidGamba/go-getoptions/blob/bake/bake/examples/website/README.adoc[here]. + +[source, go] +---- +package main + +import ( + "context" + "fmt" + + "github.com/DavidGamba/go-getoptions" +) + +// say:hello - This is a greeting +func Hello(opt *getoptions.GetOpt) getoptions.CommandFn { + var lang string + opt.StringVar(&lang, "lang", "en", opt.ValidValues("en", "es")) + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Printf("Running %s\n", opt.GetCommandName()) + + switch lang { + case "en": + fmt.Println("Hello") + case "es": + fmt.Println("Hola") + } + + return nil + } +} +---- + +Run it: + +---- +$ bake say hello +2023/04/10 14:58:34 Running Hello +Hello + +$ bake say hello --lang=es +2023/04/10 14:58:38 Running Hello +Hola +---- + +== How does it work? + +Bake is a Make like tool that allows you to define and run tasks defined in Go code under a `bake/` dir (or a `bakefiles/` dir). + +First it searches for `bake/` inside the current directory, +next it searches to see if the current directory is named `bake/`, +finally it searches for a `bake/` directory in the parent directories. +This allows to run bake from anywhere in the repo. + +Once a `bake/` dir is found, it will parse the AST of the Go files in that directory to find functions that match the proper signature. +It will then generate an entry point file that uses those functions, this file is auto-generated any time your source code changes. +Finally it will compile the binary and run it. + +Having a go binary that bake runs allows you to debug your code directly without having to worry about bake's internals. +The binary is only recompiled if the source code is changed (using https://github.com/DavidGamba/dgtools/tree/master/fsmodtime[fsmodtime]). + +The bake binary loads your functions as tasks and subtasks and makes them and their options available for completion. + +The bake Task signature is `func(opt *getoptions.GetOpt) getoptions.CommandFn`. +The functions are loaded as https://github.com/DavidGamba/go-getoptions/tree/master[go-getoptions] commands and subcommands, by parsing the comment description. + +For example: + +[source,go] +---- +// say:hello - This is a greeting +func Hello(opt *getoptions.GetOpt) getoptions.CommandFn { +---- + +If there is no comment found for the function, the function name will be automatically converted to kebab case. + +The above function will be generate two commands, one for `say` and one for `hello`. +The description for the `hello` command will be `This is a greeting`. + +Since the tasks are added to the bake command's `go-getoptions` instance, completions are automatically generated. + +It also adds the task to the global task map, the task will automatically be added as `say:hello`. +This allows to generate custom task graphs using https://github.com/DavidGamba/go-getoptions/blob/master/dag/README.adoc[go-getoptions DAG]. + +== Debugging + +To debug your program go to the `bake/` directory and run `bake` and you should see the `bake` binary. + +Set your IDE Debugger to run `./bake` with the proper arguments for your task. + +To print `bake` traces, set the env var `BAKE_TRACE=true`. + +== ROADMAP + +* Currently not all `go-getoptions` types are supported. + +* Helper for automated cancellation on timeout when passing -t flag. diff --git a/bake/ast.go b/bake/ast.go new file mode 100644 index 0000000..514a12d --- /dev/null +++ b/bake/ast.go @@ -0,0 +1,126 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "bytes" + "fmt" + "go/ast" + "go/parser" + "go/printer" + "go/token" + "iter" + "strings" + + "golang.org/x/tools/go/packages" +) + +type ParsedFile struct { + file string + fset *token.FileSet + f *ast.File +} + +// Requires GOEXPERIMENT=rangefunc +func parsedFiles(dir string) iter.Seq2[ParsedFile, error] { + return func(yield func(ParsedFile, error) bool) { + cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax, Dir: dir} + pkgs, err := packages.Load(cfg, ".") + if err != nil { + yield(ParsedFile{}, fmt.Errorf("failed to load packages: %w", err)) + return + } + for _, pkg := range pkgs { + // Logger.Println(pkg.ID, pkg.GoFiles) + for _, file := range pkg.GoFiles { + if strings.Contains(file, "generated") { + continue + } + p := ParsedFile{} + // Logger.Printf("file: %s\n", file) + // parse file + fset := token.NewFileSet() + fset.AddFile(file, fset.Base(), len(file)) + p.file = file + p.fset = fset + f, err := parser.ParseFile(fset, file, nil, parser.ParseComments) + if err != nil { + yield(p, fmt.Errorf("failed to parse file: %w", err)) + return + } + p.f = f + if !yield(p, nil) { + return + } + } + } + } +} + +type FnDecl struct { + Name string + Description string + + Node ast.Node + ParsedFile ParsedFile + Type string +} + +// Requires GOEXPERIMENT=rangefunc +func AstFns(dir string) iter.Seq2[FnDecl, error] { + return func(yield func(FnDecl, error) bool) { + for p, err := range parsedFiles(dir) { + if err != nil { + yield(FnDecl{}, err) + return + } + fnDecl := FnDecl{ParsedFile: p} + + doneYield := false + // Iterate through every node in the file + ast.Inspect(p.f, func(n ast.Node) bool { + if doneYield { + return false + } + fnDecl.Node = n + switch x := n.(type) { + // Check function declarations for exported functions + case *ast.FuncDecl: + if x.Name.IsExported() { + fnDecl.Name = x.Name.Name + fnDecl.Description = x.Doc.Text() + var buf bytes.Buffer + printer.Fprint(&buf, p.fset, x.Type) + fnDecl.Type = buf.String() + if !yield(fnDecl, nil) { + doneYield = true + return false + } + } + } + return true + }) + + if doneYield { + return + } + } + } +} + +func PrintFuncDecl(dir string) error { + for fnDecl, err := range AstFns(dir) { + if err != nil { + return err + } + fmt.Printf("file: %s\n\tname: %s\n\ttype: %s\n\tdesc: %s\n", fnDecl.ParsedFile.file, fnDecl.Name, fnDecl.Type, fnDecl.Description) + } + + return nil +} diff --git a/bake/ast_getoptions.go b/bake/ast_getoptions.go new file mode 100644 index 0000000..d0ccf98 --- /dev/null +++ b/bake/ast_getoptions.go @@ -0,0 +1,135 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "bytes" + "context" + "go/ast" + "go/printer" + "iter" + "regexp" + "strings" + + "github.com/DavidGamba/go-getoptions" +) + +// The goal is to be able to find the getoptions.CommandFn calls. +// Also, we need to inspect the function and get the opt. calls to know what options are being used. +// +// func Asciidoc(opt *getoptions.GetOpt) getoptions.CommandFn { +// opt.String("lang", "en", opt.ValidValues("en", "es")) +// opt.String("hello", "world") +// opt.String("hola", "mundo") +// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { +func LoadAst(ctx context.Context, opt *getoptions.GetOpt, dir string) (*OptTree, error) { + ot := NewOptTree(opt) + + for getOptFn, err := range AstGetoptionFns(ctx, dir) { + if err != nil { + return ot, err + } + + cmd, err := ot.AddCommand(getOptFn.Name, getOptFn.DescName, getOptFn.Description) + if err != nil { + return ot, err + } + err = addOptionsToCMD(getOptFn, cmd, getOptFn.DescName) + if err != nil { + return ot, err + } + } + + return ot, nil +} + +type GetOptFn struct { + FnDecl + + DescName string + OptFieldName string +} + +// The goal is to be able to find the getoptions.CommandFn calls. +// Also, we need to inspect the function and get the opt. calls to know what options are being used. +// +// func Asciidoc(opt *getoptions.GetOpt) getoptions.CommandFn { +// opt.String("lang", "en", opt.ValidValues("en", "es")) +// opt.String("hello", "world") +// opt.String("hola", "mundo") +// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { +func AstGetoptionFns(ctx context.Context, dir string) iter.Seq2[GetOptFn, error] { + return func(yield func(GetOptFn, error) bool) { + // Regex for description: fn-name - description + re := regexp.MustCompile(`^\w\S+ -`) + + LOOP: + for fnDecl, err := range AstFns(dir) { + if err != nil { + yield(GetOptFn{}, err) + return + } + getOptFn := GetOptFn{FnDecl: fnDecl} + + x := fnDecl.Node.(*ast.FuncDecl) + + getOptFn.Description = strings.TrimSpace(getOptFn.Description) + + // Expect function of type: + // func Name(opt *getoptions.GetOpt) getoptions.CommandFn + + // Check Params + // Expect opt *getoptions.GetOpt + if len(x.Type.Params.List) != 1 { + continue + } + for _, param := range x.Type.Params.List { + name := param.Names[0].Name + var buf bytes.Buffer + printer.Fprint(&buf, fnDecl.ParsedFile.fset, param.Type) + // Logger.Printf("name: %s, %s\n", name, buf.String()) + if buf.String() != "*getoptions.GetOpt" { + continue LOOP + } + getOptFn.OptFieldName = name + } + + // Check Results + // Expect getoptions.CommandFn + if len(x.Type.Results.List) != 1 { + continue + } + for _, result := range x.Type.Results.List { + var buf bytes.Buffer + printer.Fprint(&buf, fnDecl.ParsedFile.fset, result.Type) + // Logger.Printf("result: %s\n", buf.String()) + if buf.String() != "getoptions.CommandFn" { + continue LOOP + } + } + + // TODO: The yield probably goes here + // Add function to OptTree + if getOptFn.Description != "" { + // Logger.Printf("description '%s'\n", description) + if re.MatchString(getOptFn.Description) { + // Get first word from string + getOptFn.DescName = strings.Split(getOptFn.Description, " ")[0] + getOptFn.Description = strings.TrimPrefix(getOptFn.Description, getOptFn.DescName+" -") + getOptFn.Description = strings.TrimSpace(getOptFn.Description) + } + } else { + getOptFn.DescName = camelToKebab(getOptFn.Name) + } + if !yield(getOptFn, nil) { + return + } + } + } +} diff --git a/bake/ast_handle_options.go b/bake/ast_handle_options.go new file mode 100644 index 0000000..fb995e1 --- /dev/null +++ b/bake/ast_handle_options.go @@ -0,0 +1,228 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "bytes" + "fmt" + "go/ast" + "go/printer" + "os" + "strconv" + + "github.com/DavidGamba/go-getoptions" +) + +func addOptionsToCMD(getOptFn GetOptFn, cmd *getoptions.GetOpt, name string) error { + Logger.Printf("Adding options to %s\n", name) + + var outerErr error + // Check for Expressions of opt type + ast.Inspect(getOptFn.Node, func(n ast.Node) bool { + switch x := n.(type) { + case *ast.BlockStmt: + for _, stmt := range x.List { + var buf bytes.Buffer + printer.Fprint(&buf, getOptFn.ParsedFile.fset, stmt) + // We are expecting the expression before the return function + _, ok := stmt.(*ast.ReturnStmt) + if ok { + return false + } + // Logger.Printf("stmt: %s\n", buf.String()) + exprStmt, ok := stmt.(*ast.ExprStmt) + if !ok { + continue + } + // spew.Dump(exprStmt) + + // Check for CallExpr + ast.Inspect(exprStmt, func(n ast.Node) bool { + switch x := n.(type) { + case *ast.CallExpr: + fun, ok := x.Fun.(*ast.SelectorExpr) + if !ok { + return false + } + xIdent, ok := fun.X.(*ast.Ident) + if !ok { + return false + } + if xIdent.Name != getOptFn.OptFieldName { + return false + } + // Logger.Printf("handling %s.%s\n", xIdent.Name, fun.Sel.Name) + + switch fun.Sel.Name { + case "String", "StringOptional", "Int", "IntOptional", "Increment", "Float64", "Float64Optional": + mfns := []getoptions.ModifyFn{} + if len(x.Args) > 2 { + mfns = handleOptionModifiers(cmd, getOptFn.OptFieldName, x.Args[2:]) + } + name, defaultValue, err := extractNameAndDefault(n, 0) + if err != nil { + outerErr = err + return false + } + switch fun.Sel.Name { + case "String": + cmd.String(name, defaultValue, mfns...) + case "StringOptional": + cmd.StringOptional(name, defaultValue, mfns...) + case "Int": + x, err := strconv.Atoi(defaultValue) + if err != nil { + x = 0 + } + cmd.Int(name, x, mfns...) + case "IntOptional": + x, err := strconv.Atoi(defaultValue) + if err != nil { + x = 0 + } + cmd.IntOptional(name, x, mfns...) + case "Increment": + x, err := strconv.Atoi(defaultValue) + if err != nil { + x = 0 + } + cmd.Increment(name, x, mfns...) + case "Float64": + x, err := strconv.ParseFloat(defaultValue, 64) + if err != nil { + x = 0.0 + } + cmd.Float64(name, x, mfns...) + case "Float64Optional": + x, err := strconv.ParseFloat(defaultValue, 64) + if err != nil { + x = 0.0 + } + cmd.Float64Optional(name, x, mfns...) + } + + case "StringVar": + mfns := []getoptions.ModifyFn{} + if len(x.Args) > 3 { + mfns = handleOptionModifiers(cmd, getOptFn.OptFieldName, x.Args[3:]) + } + name, defaultValue, err := extractNameAndDefault(n, 1) + if err != nil { + outerErr = err + return false + } + cmd.String(name, defaultValue, mfns...) + } + + return false + } + return true + }) + } + } + return true + }) + return outerErr +} + +func extractNameAndDefault(n ast.Node, offset int) (string, string, error) { + x := n.(*ast.CallExpr) + name, err := extractName(x.Args, offset) + if err != nil { + return "", "", err + } + defaultValue, err := extractDefault(x.Args, offset) + if err != nil { + return "", "", err + } + return name, defaultValue, nil +} + +func extractName(args []ast.Expr, offset int) (string, error) { + // First argument is the Name + if len(args) < 1+offset { + return "", fmt.Errorf("missing name argument") + } + name, err := strconv.Unquote(args[0+offset].(*ast.BasicLit).Value) + if err != nil { + name = args[0+offset].(*ast.BasicLit).Value + } + return name, nil +} + +func extractDefault(args []ast.Expr, offset int) (string, error) { + // Second argument is the Default + if len(args) < 2+offset { + return "", fmt.Errorf("missing default argument") + } + defaultValue, err := strconv.Unquote(args[1+offset].(*ast.BasicLit).Value) + if err != nil { + defaultValue = args[1+offset].(*ast.BasicLit).Value + } + return defaultValue, nil +} + +func handleOptionModifiers(cmd *getoptions.GetOpt, optFieldName string, args []ast.Expr) []getoptions.ModifyFn { + mfns := []getoptions.ModifyFn{} + for _, arg := range args { + callE, ok := arg.(*ast.CallExpr) + if !ok { + continue + } + fun, ok := callE.Fun.(*ast.SelectorExpr) + if !ok { + continue + } + xIdent, ok := fun.X.(*ast.Ident) + if !ok { + continue + } + if xIdent.Name != optFieldName { + continue + } + // Logger.Printf("\t%s.%s\n", xIdent.Name, fun.Sel.Name) + if fun.Sel.Name == "SetCalled" { + // TODO: SetCalled function receives a bool + fmt.Fprintf(os.Stderr, "WARNING: bake: SetCalled is not implemented\n") + continue + } + values := []string{} + for _, arg := range callE.Args { + // Logger.Printf("Value: %s\n", arg.(*ast.BasicLit).Value) + value, err := strconv.Unquote(arg.(*ast.BasicLit).Value) + if err != nil { + value = arg.(*ast.BasicLit).Value + } + values = append(values, value) + } + switch fun.Sel.Name { + case "Alias": + mfns = append(mfns, cmd.Alias(values...)) + case "ArgName": + if len(values) > 0 { + mfns = append(mfns, cmd.ArgName(values[0])) + } + case "Description": + if len(values) > 0 { + mfns = append(mfns, cmd.Description(values[0])) + } + case "GetEnv": + if len(values) > 0 { + mfns = append(mfns, cmd.GetEnv(values[0])) + } + case "Required": + mfns = append(mfns, cmd.Required(values...)) + case "SuggestedValues": + mfns = append(mfns, cmd.SuggestedValues(values...)) + case "ValidValues": + mfns = append(mfns, cmd.ValidValues(values...)) + } + } + return mfns +} diff --git a/bake/build.env b/bake/build.env new file mode 100644 index 0000000..af74211 --- /dev/null +++ b/bake/build.env @@ -0,0 +1,2 @@ +# Using the golang rangefunc experiment with 1.22 +GOEXPERIMENT=rangefunc diff --git a/bake/build.go b/bake/build.go new file mode 100644 index 0000000..3fc28ac --- /dev/null +++ b/bake/build.go @@ -0,0 +1,138 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "text/template" + + "github.com/DavidGamba/dgtools/buildutils" + "github.com/DavidGamba/dgtools/fsmodtime" + "github.com/DavidGamba/dgtools/run" +) + +func buildBinary(dir string) error { + files, modified, err := fsmodtime.Target(os.DirFS(dir), + []string{"bake"}, + []string{"*.go", "go.mod", "go.sum"}) + if err != nil { + return err + } + if modified { + Logger.Printf("Found modifications on %v, rebuilding binary...\n", files) + _ = run.CMD("go", "get").Dir(dir).Log().Run() + err = run.CMD("go", "build").Dir(dir).Log().Run() + if err != nil { + os.Remove(filepath.Join(dir, "bake")) + return fmt.Errorf("failed to build binary: %w", err) + } + } + return nil +} + +var ErrNotFound = fmt.Errorf("not found") + +// findBakeDir - searches for bakefiles/ dir first then for bake/ dir. +// This allows me to use bake in this repo. +func findBakeDir(ctx context.Context) (string, error) { + wd, err := os.Getwd() + if err != nil { + return ".", fmt.Errorf("failed to get working directory: %w", err) + } + Logger.Printf("Working directory: %s\n", wd) + + // First case, bake folder lives in CWD + // This has higher priority to allow me to have a bake folder for bake itself. + dir := filepath.Join(wd, "bakefiles") + if fi, err := os.Stat(dir); err == nil && fi.Mode().IsDir() { + return dir, nil + } + + // Second case, we are withing the bake folder + base := filepath.Base(wd) + if base == "bakefiles" { + return ".", nil + } + + // Third case, search for bake folder in parent directories + d, err := buildutils.FindDirUpwards(ctx, "bakefiles") + if err == nil { + return d, nil + } + if err != nil { + if !errors.Is(err, buildutils.ErrNotFound) { + return ".", fmt.Errorf("failed to find bake folder: %w", err) + } + } + + // First case, bake folder lives in CWD + dir = filepath.Join(wd, "bake") + if fi, err := os.Stat(dir); err == nil && fi.Mode().IsDir() { + return dir, nil + } + + // Second case, we are withing the bake folder + if base == "bake" { + return ".", nil + } + + // Third case, search for bake folder in parent directories + d, err = buildutils.FindDirUpwards(ctx, "bake") + if err == nil { + return d, nil + } + if err != nil { + if !errors.Is(err, buildutils.ErrNotFound) { + return ".", fmt.Errorf("failed to find bake folder: %w", err) + } + } + return ".", ErrNotFound +} + +func GenerateMainFile(ot *OptTree, dir string) error { + files, modified, err := fsmodtime.Target(os.DirFS(dir), + []string{generatedMainFilename}, + []string{"*.go", "go.mod", "go.sum"}) + if err != nil { + return err + } + + binaryExists := true + if _, err := os.Stat(filepath.Join(dir, "bake")); os.IsNotExist(err) { + binaryExists = false + } + + if !modified && binaryExists { + return nil + } + Logger.Printf("Found source modifications on %v, regenerating template...\n", files) + + // Render template + tmpl, err := template.ParseFS(templates, "templates/main.go.gotmpl") + if err != nil { + return fmt.Errorf("failed to parse template: %w", err) + } + data := map[string]string{ + "Tree": ot.String(), + } + // get writer to write to main.go + w, err := os.Create(filepath.Join(dir, generatedMainFilename)) + if err != nil { + return fmt.Errorf("failed to create file: %w", err) + } + err = tmpl.Execute(w, data) + if err != nil { + return fmt.Errorf("failed to execute template: %w", err) + } + return run.CMD("go", "fmt", generatedMainFilename).Dir(dir).Log().Run() +} diff --git a/bake/examples/website/.gitignore b/bake/examples/website/.gitignore new file mode 100644 index 0000000..ca84870 --- /dev/null +++ b/bake/examples/website/.gitignore @@ -0,0 +1,2 @@ +public/ +website diff --git a/bake/examples/website/README.adoc b/bake/examples/website/README.adoc new file mode 100644 index 0000000..04ee8fc --- /dev/null +++ b/bake/examples/website/README.adoc @@ -0,0 +1,37 @@ += Bake website example + +An Bake example showing most of bake's features. + +The application itself is: + +* A simple go web server that serves anything under `public/` under `:8080`. + +* A simple Asciidoc file that is rendered to the `index.html` file. +There is one English and one Spanish version. + +* A simple Graphviz dot diagram that is rendered to the `diagram.png` file and included in the `index.html`. + +The bake tasks consist of: + +`build:go`:: A task to build the web server. +`serve`:: A task to serve the website using the web server. +`build:index`:: A task to build the asciidoc file. +`build:diagram`:: A task to build the dot diagram. +`build:all`:: A task to build all build tasks showcasing the dependency system. +`clean`:: A task to clean build artifacts. + +The features shown by this example: + +* Bake autocompletes tasks and option flags. +* Bake keeps track of all declared tasks and allows for custom task graphs (See the `build:all` task). +* Leverage https://github.com/DavidGamba/dgtools/tree/master/fsmodtime[fsmodtime], a library with functions that allow to run tasks only when the sources have been updated or the targets do not yet exist. +* Leverage https://github.com/DavidGamba/dgtools/tree/master/run[run], an easy to use `os/exec` wrapper. +* Leverage https://github.com/DavidGamba/dgtools/tree/master/buildutils[buildutils], functions used when writing build automation. + +The above are all the pieces you need to build complex custom build systems. + +== Step by step guide + +Follow the installation steps from the https://github.com/DavidGamba/go-getoptions/blob/bake/bake/README.adoc[README]. + +Run: `bake` from the `go-getoptions/bake/examples/website` directory. diff --git a/bake/examples/website/bake/go.mod b/bake/examples/website/bake/go.mod new file mode 100644 index 0000000..2e1ca1c --- /dev/null +++ b/bake/examples/website/bake/go.mod @@ -0,0 +1,10 @@ +module bake + +go 1.22.3 + +require ( + github.com/DavidGamba/dgtools/buildutils v0.4.0 // indirect + github.com/DavidGamba/dgtools/fsmodtime v0.2.0 // indirect + github.com/DavidGamba/dgtools/run v0.9.0 // indirect + github.com/DavidGamba/go-getoptions v0.30.0 // indirect +) diff --git a/bake/examples/website/bake/go.sum b/bake/examples/website/bake/go.sum new file mode 100644 index 0000000..e7764b8 --- /dev/null +++ b/bake/examples/website/bake/go.sum @@ -0,0 +1,8 @@ +github.com/DavidGamba/dgtools/buildutils v0.4.0 h1:9qg5/FJaEKwYYiaRh2fVKgJCPcT2U0u4hTTjQvR4VE0= +github.com/DavidGamba/dgtools/buildutils v0.4.0/go.mod h1:gEikilH0xsJVMydPErNv4mlUPhJGFT5k6v5Ss+Fn+d4= +github.com/DavidGamba/dgtools/fsmodtime v0.2.0 h1:2GnxhUIsaNzCj1LLrxhKA3wWv9z0KKrY68tIVcXo/QY= +github.com/DavidGamba/dgtools/fsmodtime v0.2.0/go.mod h1:ruwqMvW2pWDbSQlAupP7F0QaojfbuXPyUOUKR4Ev3pQ= +github.com/DavidGamba/dgtools/run v0.9.0 h1:Hg0v4ExUMd6Vzf9x9Bqr2yxreZtZpqlcAi8tI86QtIM= +github.com/DavidGamba/dgtools/run v0.9.0/go.mod h1:GVGYL0p5hdBaQ9uIAslXh1g1TTfr0igMSDVTwhhy9q4= +github.com/DavidGamba/go-getoptions v0.30.0 h1:8x69Fc8k/mEWVE0GknpwQ3uGj56MXOUp17egPxCEAG4= +github.com/DavidGamba/go-getoptions v0.30.0/go.mod h1:zE97E3PR9P3BI/HKyNYgdMlYxodcuiC6W68KIgeYT84= diff --git a/bake/examples/website/bake/main.go b/bake/examples/website/bake/main.go new file mode 100644 index 0000000..efab2e3 --- /dev/null +++ b/bake/examples/website/bake/main.go @@ -0,0 +1,233 @@ +package main + +import ( + "context" + "fmt" + "os" + "path/filepath" + + "github.com/DavidGamba/dgtools/buildutils" + "github.com/DavidGamba/dgtools/fsmodtime" + "github.com/DavidGamba/dgtools/run" + "github.com/DavidGamba/go-getoptions" + "github.com/DavidGamba/go-getoptions/dag" +) + +func Nothing(s string) error { + return nil +} + +// build - Build tasks +func Build(opt *getoptions.GetOpt) getoptions.CommandFn { + // Empty task used to group tasks. + // It is not required (it would be autogenerated), except for the `Build tasks` description. + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + fmt.Fprint(os.Stderr, opt.Help()) + return nil + } +} + +// build:gobuild - Builds go project +func Go(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:go") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"website"}, []string{"go.mod", "go.sum", "*.go"}) + if err != nil { + return fmt.Errorf("failed to detect changes: %w", err) + } + if !modified { + return nil + } + Logger.Printf("Modified files: %v\n", files) + + err = run.CMD("go", "build").Log().Run() + if err != nil { + return fmt.Errorf("failed to build go project: %w", err) + } + + return nil + } +} + +// build:diagram - Builds diagram +func Dot(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:diagram") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"public/diagram.png"}, []string{"diagram.dot"}) + if err != nil { + return fmt.Errorf("failed to detect changes: %w", err) + } + if !modified { + return nil + } + Logger.Printf("Modified files: %v\n", files) + + _ = os.MkdirAll("public", 0755) + err = run.CMD("dot", "-Tpng", "-opublic/diagram.png", "diagram.dot").Log().Run() + if err != nil { + return fmt.Errorf("failed to build diagram: %w", err) + } + + return nil + } +} + +// build:index - Builds index page +// NOTE: Run clean before changing the language since no changes will be detected. +func Asciidoc(opt *getoptions.GetOpt) getoptions.CommandFn { + opt.String("lang", "en", opt.ValidValues("en", "es"), opt.Description("Language")) + opt.String("hello", "world") + Logger.Println("Running Asciidoc prep") + opt.String("hola", "mundo") + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:diagram") + lang := opt.Value("lang").(string) + + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"public/index.html"}, []string{"index-en.adoc", "index-es.adoc"}) + if err != nil { + return fmt.Errorf("failed to detect changes: %w", err) + } + if !modified { + return nil + } + Logger.Printf("Modified files: %v\n", files) + + _ = os.MkdirAll("public", 0755) + file := "index-en.adoc" + switch lang { + case "es": + file = "index-es.adoc" + } + + err = run.CMD("asciidoctor", file, "-o", "public/index.html").Log().Run() + if err != nil { + return fmt.Errorf("failed to build diagram: %w", err) + } + + return nil + } +} + +// build:all - Builds website +func All(opt *getoptions.GetOpt) getoptions.CommandFn { + opt.String("lang", "en", opt.ValidValues("en", "es")) + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:all") + + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + g := dag.NewGraph("website") + g.TaskDependensOn(TM.Get("build:index"), TM.Get("build:diagram")) + g.AddTask(TM.Get("build:gobuild")) + + err = g.Validate(TM) + if err != nil { + return fmt.Errorf("validation: %w", err) + } + err = g.Run(ctx, opt, args) + if err != nil { + return fmt.Errorf("dag err: %w", err) + } + + return nil + } +} + +// serve - Serves website on :8080 +func Serve(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running serve") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + err = run.CMD("./website").Ctx(ctx).Log().Run() + if err != nil { + return fmt.Errorf("failed to serve: %w", err) + } + + return nil + } +} + +// clean - Clean build artifacts +func Clean(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running clean") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + _ = os.RemoveAll("public") + _ = os.Remove("website") + + return nil + } +} + +// notused - Not used because it doesn't return a getoptions.CommandFn +func NotUsed() error { + return nil +} + +// say:hello - This is a greeting +func Hello(opt *getoptions.GetOpt) getoptions.CommandFn { + var lang string + opt.StringVar(&lang, "lang", "en", opt.ValidValues("en", "es")) + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running Hello") + + switch lang { + case "en": + fmt.Println("Hello") + case "es": + fmt.Println("Hola") + } + + return nil + } +} diff --git a/bake/examples/website/diagram.dot b/bake/examples/website/diagram.dot new file mode 100644 index 0000000..c0763b3 --- /dev/null +++ b/bake/examples/website/diagram.dot @@ -0,0 +1,4 @@ +digraph graphname { + a -> b -> c; + b -> d; +} diff --git a/bake/examples/website/go.mod b/bake/examples/website/go.mod new file mode 100644 index 0000000..a31ea83 --- /dev/null +++ b/bake/examples/website/go.mod @@ -0,0 +1,3 @@ +module website + +go 1.20 diff --git a/bake/examples/website/index-en.adoc b/bake/examples/website/index-en.adoc new file mode 100644 index 0000000..c08f059 --- /dev/null +++ b/bake/examples/website/index-en.adoc @@ -0,0 +1,6 @@ += Hello World + +This is a sample page + +.Image +image::diagram.png[] diff --git a/bake/examples/website/index-es.adoc b/bake/examples/website/index-es.adoc new file mode 100644 index 0000000..976abb0 --- /dev/null +++ b/bake/examples/website/index-es.adoc @@ -0,0 +1,6 @@ += Hola Mundo + +Esta es una pagina de prueba + +.Imagen +image::diagram.png[] diff --git a/bake/examples/website/main.go b/bake/examples/website/main.go new file mode 100644 index 0000000..a93163a --- /dev/null +++ b/bake/examples/website/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" +) + +var Logger = log.New(os.Stderr, "", log.LstdFlags) + +func main() { + os.Exit(program(os.Args)) +} + +func program(args []string) int { + err := serve() + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + return 0 +} + +func serve() error { + Logger.Printf("Running") + fs := http.FileServer(http.Dir("public")) + err := http.ListenAndServe(":8080", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { + resp.Header().Add("Cache-Control", "no-cache") + fs.ServeHTTP(resp, req) + })) + if err != nil { + return err + } + return nil +} diff --git a/bake/go.mod b/bake/go.mod new file mode 100644 index 0000000..9083fda --- /dev/null +++ b/bake/go.mod @@ -0,0 +1,17 @@ +module github.com/DavidGamba/dgtools/bake + +go 1.22 + +require ( + github.com/DavidGamba/dgtools/buildutils v0.5.0 + github.com/DavidGamba/dgtools/fsmodtime v0.2.0 + github.com/DavidGamba/dgtools/run v0.9.0 + github.com/DavidGamba/go-getoptions v0.30.1-0.20240619042100-106cbf127fac + github.com/davecgh/go-spew v1.1.1 + golang.org/x/tools v0.22.0 +) + +require ( + golang.org/x/mod v0.18.0 // indirect + golang.org/x/sync v0.7.0 // indirect +) diff --git a/bake/go.sum b/bake/go.sum new file mode 100644 index 0000000..7d17148 --- /dev/null +++ b/bake/go.sum @@ -0,0 +1,20 @@ +github.com/DavidGamba/dgtools/buildutils v0.4.0 h1:9qg5/FJaEKwYYiaRh2fVKgJCPcT2U0u4hTTjQvR4VE0= +github.com/DavidGamba/dgtools/buildutils v0.4.0/go.mod h1:gEikilH0xsJVMydPErNv4mlUPhJGFT5k6v5Ss+Fn+d4= +github.com/DavidGamba/dgtools/buildutils v0.5.0 h1:RBlKWgN4LmbBo1Qbsp0oXFdwTZdC3AO0yOec3mLT1H0= +github.com/DavidGamba/dgtools/buildutils v0.5.0/go.mod h1:j7DC6tKOOoMy4s6ICP220y2jgRlIGpzLH2wXZo2WF7g= +github.com/DavidGamba/dgtools/fsmodtime v0.2.0 h1:2GnxhUIsaNzCj1LLrxhKA3wWv9z0KKrY68tIVcXo/QY= +github.com/DavidGamba/dgtools/fsmodtime v0.2.0/go.mod h1:ruwqMvW2pWDbSQlAupP7F0QaojfbuXPyUOUKR4Ev3pQ= +github.com/DavidGamba/dgtools/run v0.9.0 h1:Hg0v4ExUMd6Vzf9x9Bqr2yxreZtZpqlcAi8tI86QtIM= +github.com/DavidGamba/dgtools/run v0.9.0/go.mod h1:GVGYL0p5hdBaQ9uIAslXh1g1TTfr0igMSDVTwhhy9q4= +github.com/DavidGamba/go-getoptions v0.30.0 h1:8x69Fc8k/mEWVE0GknpwQ3uGj56MXOUp17egPxCEAG4= +github.com/DavidGamba/go-getoptions v0.30.0/go.mod h1:zE97E3PR9P3BI/HKyNYgdMlYxodcuiC6W68KIgeYT84= +github.com/DavidGamba/go-getoptions v0.30.1-0.20240619042100-106cbf127fac h1:JyEpi40bQ0ILgALsmKg4OTk5fJfONQKRjG+EzqwAWuE= +github.com/DavidGamba/go-getoptions v0.30.1-0.20240619042100-106cbf127fac/go.mod h1:zE97E3PR9P3BI/HKyNYgdMlYxodcuiC6W68KIgeYT84= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= +golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= +golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= diff --git a/bake/init.go b/bake/init.go new file mode 100644 index 0000000..8535784 --- /dev/null +++ b/bake/init.go @@ -0,0 +1,78 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "context" + "fmt" + "os" + "path/filepath" + + "github.com/DavidGamba/dgtools/run" + "github.com/DavidGamba/go-getoptions" +) + +func initRun(dir string) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + name := opt.Value("name").(string) + err := initFn(opt, name) + if err != nil { + return fmt.Errorf("failed to inspect package: %w", err) + } + return nil + } +} + +func initFn(opt *getoptions.GetOpt, dir string) error { + Logger.Printf("Initializing bake project in %s\n", dir) + os.MkdirAll(dir, 0755) + + _ = run.CMD("go", "mod", "init", "bake").Dir(dir).Log().Run() + // if err != nil { + // return fmt.Errorf("failed to initialize go mod: %w", err) + // } + _ = run.CMD("go", "work", "init").Dir(dir).Log().Env("GOWORK=off").Run() + // if err != nil { + // return fmt.Errorf("failed to initialize go work: %w", err) + // } + _ = run.CMD("go", "work", "use", ".").Dir(dir).Log().Run() + // if err != nil { + // return fmt.Errorf("failed to configure go work: %w", err) + // } + // github.com/DavidGamba/dgtools/buildutils + // github.com/DavidGamba/dgtools/fsmodtime + // github.com/DavidGamba/dgtools/run + // github.com/DavidGamba/go-getoptions + + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/dgtools/buildutils").Dir(dir).Log().Run() + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/dgtools/fsmodtime").Dir(dir).Log().Run() + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/dgtools/run").Dir(dir).Log().Run() + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/go-getoptions@command-name").Dir(dir).Log().Run() + + ot := NewOptTree(opt) + err := GenerateMainFile(ot, dir) + if err != nil { + return fmt.Errorf("failed to generate file: %w", err) + } + + return nil +} + +func bakeDirHasRequiredFiles(dir string) bool { + // check that files exist: go.mod, go.sum, go.work + for _, file := range []string{"go.mod", "go.sum", "go.work"} { + file = filepath.Join(dir, file) + if _, err := os.Stat(file); os.IsNotExist(err) { + Logger.Printf("Missing file %s\n", file) + return false + } + } + + return true +} diff --git a/bake/main.go b/bake/main.go new file mode 100644 index 0000000..2bba92a --- /dev/null +++ b/bake/main.go @@ -0,0 +1,166 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "context" + "embed" + "errors" + "fmt" + "io" + "log" + "os" + "path/filepath" + "runtime" + "runtime/debug" + + "github.com/DavidGamba/dgtools/buildutils" + "github.com/DavidGamba/dgtools/run" + "github.com/DavidGamba/go-getoptions" +) + +//go:embed templates/*.go.gotmpl +var templates embed.FS + +const ( + version = "0.1.0" + generatedMainFilename = "generated_bake.go" +) + +var InputArgs []string +var Dir string + +var Logger = log.New(io.Discard, "", log.LstdFlags) + +func main() { + os.Exit(program(os.Args)) +} + +func program(args []string) int { + ctx, cancel, done := getoptions.InterruptContext() + defer func() { cancel(); <-done }() + + run.Logger = Logger + + if os.Getenv("BAKE_TRACE") != "" { + Logger.SetOutput(os.Stderr) + run.Logger.SetOutput(os.Stderr) + buildutils.Logger.SetOutput(os.Stderr) + } + + opt := getoptions.New() + opt.Self("bake", "Go Build + Something like Make = Bake ¯\\_(ツ)_/¯") + opt.SetUnknownMode(getoptions.Pass) + opt.Bool("quiet", false, opt.GetEnv("QUIET")) + + dir, err := findBakeDir(ctx) + if err != nil && !errors.Is(err, ErrNotFound) { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + Dir = dir + InputArgs = args[1:] + Logger.Printf("Running bake in %s with args: %v\n", dir, InputArgs) + + requiredFilesPresent := false + if err == nil { + requiredFilesPresent = bakeDirHasRequiredFiles(dir) + } + + if requiredFilesPresent { + ot, err := LoadAst(ctx, opt, dir) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + err = GenerateMainFile(ot, dir) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + err = buildBinary(dir) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + } else { + Logger.Printf("Required files not present\n") + } + + b := opt.NewCommand("_bake", "") + + bld := b.NewCommand("list-fns", "lists all functions in the package") + bld.SetCommandFn(PrintFuncDeclRun(dir)) + + binit := b.NewCommand("init", "initialize a new bake project") + binit.String("name", "bake", opt.Description("name of the bake directory"), opt.ValidValues("bake", "bakefiles")) + binit.SetCommandFn(initRun(dir)) + + bforce := b.NewCommand("force", "force rebuild of the generated bake file and the binary on the next run") + bforce.SetCommandFn(InvalidateCache(dir)) + + bversion := b.NewCommand("version", "print the version of bake") + bversion.SetCommandFn(Version(dir)) + + opt.HelpCommand("help", opt.Alias("?")) + remaining, err := opt.Parse(args[1:]) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + + err = opt.Dispatch(ctx, remaining) + if err != nil { + if errors.Is(err, getoptions.ErrorHelpCalled) { + return 1 + } + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + return 0 +} + +func PrintFuncDeclRun(dir string) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + err := PrintFuncDecl(dir) + if err != nil { + return fmt.Errorf("failed to inspect package: %w", err) + } + return nil + } +} + +func InvalidateCache(dir string) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + fmt.Printf("Invalidating bake cache...\n") + err := buildutils.Touch(filepath.Join(dir, "go.mod")) + if err != nil { + return fmt.Errorf("failed to invalidate cache: %w", err) + } + return nil + } +} + +func Version(dir string) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + fmt.Printf("bake version %s\n", version) + fmt.Printf("go version %s\n", runtime.Version()) + info, ok := debug.ReadBuildInfo() + if !ok { + return fmt.Errorf("failed to read build info") + } + fmt.Printf("%14s %s\n", "module.path", info.Main.Path) + for _, s := range info.Settings { + if s.Value != "" { + fmt.Printf("%14s %s\n", s.Key, s.Value) + } + } + return nil + } +} diff --git a/bake/templates/main.go.gotmpl b/bake/templates/main.go.gotmpl new file mode 100644 index 0000000..0fdd748 --- /dev/null +++ b/bake/templates/main.go.gotmpl @@ -0,0 +1,68 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "errors" + "fmt" + "io" + "log" + "os" + + "github.com/DavidGamba/go-getoptions" + "github.com/DavidGamba/go-getoptions/dag" +) + +var Logger = log.New(os.Stderr, "", log.LstdFlags) + +var TM *dag.TaskMap + +func main() { + os.Exit(program(os.Args)) +} + +func program(args []string) int { + TM = dag.NewTaskMap() + + opt := getoptions.New() + opt.SetUnknownMode(getoptions.Pass) + opt.Bool("quiet", false, opt.GetEnv("QUIET")) + + loadFns(opt) + + opt.HelpCommand("help", opt.Alias("?")) + remaining, err := opt.Parse(args[1:]) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + return 1 + } + if opt.Called("quiet") { + Logger.SetOutput(io.Discard) + } + + ctx, cancel, done := getoptions.InterruptContext() + defer func() { cancel(); <-done }() + + err = opt.Dispatch(ctx, remaining) + if err != nil { + if errors.Is(err, getoptions.ErrorHelpCalled) { + return 1 + } + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + if errors.Is(err, getoptions.ErrorParsing) { + fmt.Fprintf(os.Stderr, "\n"+opt.Help()) + } + return 1 + } + return 0 +} + +func loadFns(opt *getoptions.GetOpt) { + {{.Tree}} +} diff --git a/bake/tree.go b/bake/tree.go new file mode 100644 index 0000000..2ba89e2 --- /dev/null +++ b/bake/tree.go @@ -0,0 +1,174 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "context" + "fmt" + "path/filepath" + "regexp" + "strings" + + "github.com/DavidGamba/dgtools/run" + "github.com/DavidGamba/go-getoptions" +) + +// type TaskDefinitionFn func(ctx context.Context, opt *getoptions.GetOpt) error +// type TaskFn func(*getoptions.GetOpt) getoptions.CommandFn + +type OptTree struct { + Root *OptNode +} + +type OptNode struct { + Name string + Opt *getoptions.GetOpt + Children map[string]*OptNode + Parent string + DescName string + Description string +} + +func NewOptTree(opt *getoptions.GetOpt) *OptTree { + return &OptTree{ + Root: &OptNode{ + Name: "", + Parent: "", + Opt: opt, + DescName: "", + Description: "", + Children: make(map[string]*OptNode), + }, + } +} + +// Regex for description: fn-name - description +var descriptionRe = regexp.MustCompile(`^\w\S+ -`) + +func (ot *OptTree) AddCommand(name, descName, description string) (*getoptions.GetOpt, error) { + Logger.Printf("Adding command %s with function %s\n", descName, name) + keys := strings.Split(descName, ":") + node := ot.Root + var cmd *getoptions.GetOpt + for i, key := range keys { + n, ok := node.Children[key] + if ok { + // Logger.Printf("key: %v already defined, parent: %s\n", key, node.Name) + node = n + cmd = n.Opt + if len(keys) == i+1 { + cmd.Self(key, description) + } + continue + } + // Logger.Printf("key: %v not defined, parent: %s\n", key, node.Name) + desc := "" + if len(keys) == i+1 { + desc = description + } + err := validateCmdName(key, descName) + if err != nil { + return nil, err + } + cmd = node.Opt.NewCommand(key, desc) + node.Children[key] = &OptNode{ + Name: "", + Parent: node.DescName, + Opt: cmd, + Children: make(map[string]*OptNode), + Description: desc, + DescName: key, + } + if len(keys) == i+1 { + node.Children[key].Name = name + cmd.SetCommandFn(func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Printf("Running %v from %s\n", InputArgs, Dir) + // filepath.Join removes the ./ if Dir is . + // Need to ensure that it is running the local binary, not the one in the PATH + cmd := "./bake" + if Dir != "." { + cmd = filepath.Join(Dir, "bake") + } + c := []string{cmd} + run.CMD(append(c, InputArgs...)...).Log().Run() + return nil + }) + } + node = node.Children[key] + } + return cmd, nil +} + +var golangKeywords = map[string]struct{}{ + "break": {}, + "default": {}, + "func": {}, + "interface": {}, + "go": {}, + "select": {}, + "case": {}, + "defer": {}, + "goto": {}, + "map": {}, + "struct": {}, + "chan": {}, + "else": {}, + "if": {}, + "package": {}, + "switch": {}, + "const": {}, + "fallthrough": {}, + "import": {}, + "range": {}, + "type": {}, + "continue": {}, + "for": {}, + "return": {}, + "var": {}, +} + +func validateCmdName(name, descName string) error { + // if command name matches a golang keyword, return an error + if _, ok := golangKeywords[name]; ok { + return fmt.Errorf("command name '%s' in '%s' is a golang keyword", name, descName) + } + return nil +} + +func (ot *OptTree) String() string { + return ot.Root.String() +} + +func (on *OptNode) String() string { + out := "" + parent := on.Parent + if parent == "" { + parent = "opt" + } + + if on.DescName != "" { + out += fmt.Sprintf("%s := %s.NewCommand(\"%s\", `%s`)\n", on.DescName, parent, on.DescName, on.Description) + } + + if on.Name != "" { + out += fmt.Sprintf("%sFn := %s(%s)\n", on.DescName, on.Name, on.DescName) + out += fmt.Sprintf("%s.SetCommandFn(%sFn)\n", on.DescName, on.DescName) + + // TODO: This is not considering more than two levels of commands + if parent == "opt" { + out += fmt.Sprintf("TM.Add(\"%s\", %sFn)\n\n", on.DescName, on.DescName) + } else { + out += fmt.Sprintf("TM.Add(\"%s:%s\", %sFn)\n\n", parent, on.DescName, on.DescName) + } + } + for _, child := range on.Children { + out += child.String() + } + return out +} diff --git a/bake/utils.go b/bake/utils.go new file mode 100644 index 0000000..0016ce6 --- /dev/null +++ b/bake/utils.go @@ -0,0 +1,25 @@ +// This file is part of bake. +// +// Copyright (C) 2023-2024 David Gamba Rios +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package main + +import ( + "bytes" + "unicode" +) + +func camelToKebab(camel string) string { + var buffer bytes.Buffer + for i, ch := range camel { + if unicode.IsUpper(ch) && i > 0 && !unicode.IsUpper([]rune(camel)[i-1]) { + buffer.WriteRune('-') + } + buffer.WriteRune(unicode.ToLower(ch)) + } + return buffer.String() +}