github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ci/packages/generate.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package packages
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"github.com/magefile/mage/mg"
    25  	"github.com/magefile/mage/sh"
    26  	"github.com/shurcooL/vfsgen"
    27  )
    28  
    29  // Generate recreates dynamic project parts which changes time to time.
    30  func Generate() {
    31  	mg.Deps(GenerateProtobuf, GenerateSwagger)
    32  
    33  	// Doc generation should occur after swagger generation
    34  	mg.Deps(GenerateDocs)
    35  }
    36  
    37  // GenerateProtobuf generates Protobuf models.
    38  func GenerateProtobuf() error {
    39  	mg.Deps(GetProtobuf)
    40  
    41  	if err := sh.Run("protoc", "-I=.", "--go_out=./pb", "./pb/ping.proto"); err != nil {
    42  		return err
    43  	}
    44  	if err := sh.Run("protoc", "-I=.", "--go_out=./pb", "./pb/p2p.proto"); err != nil {
    45  		return err
    46  	}
    47  	if err := sh.Run("protoc", "-I=.", "--go_out=./pb", "./pb/session.proto"); err != nil {
    48  		return err
    49  	}
    50  	return sh.Run("protoc", "-I=.", "--go_out=./pb", "./pb/payment.proto")
    51  }
    52  
    53  // GetProtobuf installs protobuf golang compiler.
    54  func GetProtobuf() error {
    55  	err := sh.RunV("go", "install", "google.golang.org/protobuf/cmd/protoc-gen-go@v1.25.0")
    56  	if err != nil {
    57  		fmt.Println("could not go get 'protoc-gen-go'")
    58  		return err
    59  	}
    60  	return nil
    61  }
    62  
    63  // GenerateSwagger Tequilapi Swagger specification.
    64  func GenerateSwagger() error {
    65  	mg.Deps(GetSwagger)
    66  
    67  	return sh.RunV("swagger", "generate", "spec", "-o", "tequilapi/docs/swagger.json", "--scan-models", "-x", `\Agithub\.com/mysteriumnetwork/feedback(/[^/]*)*\z`)
    68  }
    69  
    70  // GenerateDocs generates Tequilapi documentation pages.
    71  // Based on Redoc template for swagger - https://github.com/Redocly/redoc.
    72  func GenerateDocs() error {
    73  	err := vfsgen.Generate(
    74  		http.Dir("./tequilapi/docs"),
    75  		vfsgen.Options{
    76  			Filename:     "tequilapi/endpoints/assets/docs.go",
    77  			PackageName:  "assets",
    78  			VariableName: "DocsAssets",
    79  		},
    80  	)
    81  	if err != nil {
    82  		return fmt.Errorf("could not generate documentation assets: %w", err)
    83  	}
    84  	return nil
    85  }
    86  
    87  // GetSwagger installs swagger tool.
    88  func GetSwagger() error {
    89  	err := sh.RunV("go", "install", "github.com/go-swagger/go-swagger/cmd/swagger@v0.30.4")
    90  	if err != nil {
    91  		fmt.Println("could not go get swagger")
    92  		return err
    93  	}
    94  	return nil
    95  }