Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-8551 - module generation in CI #4315

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ default: build lint server
setup:
bash etc/setup.sh

build: build-web build-go

build-go:
go build ./...

GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
bin/$(GOOS)-$(GOARCH)/viam-cli:

.PHONY: modulegen
modulegen:
OS=$(GOOS) ARCH=$(GOARCH) ./cli/modulegen/install.sh

bin/$(GOOS)-$(GOARCH)/viam-cli: modulegen
go build $(LDFLAGS) -tags osusergo,netgo -o $@ ./cli/viam

.PHONY: cli
cli: bin/$(GOOS)-$(GOARCH)/viam-cli

build: build-web build-go

build-go: cli/modulegen/.__module_gen
go build ./...

.PHONY: cli-ci
cli-ci: bin/$(GOOS)-$(GOARCH)/viam-cli
if [ -n "$(CI_RELEASE)" ]; then \
Expand Down
11 changes: 11 additions & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,17 @@ Copy multiple files from the machine to a local destination with recursion and k
Usage: "manage your modules in Viam's registry",
HideHelpCommand: true,
Subcommands: []*cli.Command{
{
Name: "experimental",
Usage: "Experimental module features. These features are under active development and may change at any time; use with caution",
Subcommands: []*cli.Command{
{
Name: "generate",
Usage: "generate module boilerplate",
Action: ModuleBoilerplateGenerationAction,
},
},
},
{
Name: "create",
Usage: "create & register a module on app.viam.com",
Expand Down
96 changes: 96 additions & 0 deletions cli/module_boilerplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Package cli contains all business logic needed by the CLI command.
package cli

import (
"bytes"
_ "embed"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"

"github.com/urfave/cli/v2"
goutils "go.viam.com/utils"
)

//go:embed modulegen/.__module_gen
var executable []byte

func writeExecutableFile(fileName string) error {
if err := os.WriteFile(fileName, executable, 0o600); err != nil {
return err
}
return nil
}

func removeFile(fileName string) error {
return os.Remove(fileName)
}

// ModuleBoilerplateGenerationAction is the corresponding action for 'module generate'.
func ModuleBoilerplateGenerationAction(*cli.Context) (err error) {
fileName := goutils.RandomAlphaString(8)
if err = writeExecutableFile(fileName); err != nil {
return err
}
defer func() {
err = removeFile(fileName)
}()

currDir, err := os.Getwd()
if err != nil {
return err
}
path := filepath.Join(currDir, fileName)

//nolint:gosec
cmd := exec.Command(path)
stdIn, err := cmd.StdinPipe()
if err != nil {
return err
}

stdOut, err := cmd.StdoutPipe()
if err != nil {
return err
}

var stderr bytes.Buffer
cmd.Stderr = &stderr

if err = cmd.Start(); err != nil {
return fmt.Errorf("%s: %s", err.Error(), stderr.String())
}

for {
buf := make([]byte, 512)
n, err := stdOut.Read(buf)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return err
}
if _, err = os.Stdout.Write(buf[:n]); err != nil {
return err
}
var input string
if _, err = fmt.Scanln(&input); err != nil {
return err
}
input = fmt.Sprintf("%s\n", input)
if _, err = stdIn.Write([]byte(input)); err != nil {
return err
}
}

if err = stdIn.Close(); err != nil {
return err
}
if err = cmd.Wait(); err != nil {
return err
}
return err
}
3 changes: 3 additions & 0 deletions cli/modulegen/.__module_gen
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a dummy file to allow `go build` to run without creating a python dependency. It
will be overwritten by running `make cli` from directory root and should not be interacted
with directly.
13 changes: 13 additions & 0 deletions cli/modulegen/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

if [[ $OS = "windows" ]]; then
curl -sL -o ./cli/modulegen/.__module_gen https://github.com/viamrobotics/module-template-generator/releases/latest/download/windows-main.exe
elif [[ $OS = "darwin" && $ARCH = "arm64" ]]; then
curl -sL -o ./cli/modulegen/.__module_gen https://github.com/viamrobotics/module-template-generator/releases/latest/download/macosx_arm64-main
elif [[ $OS = "darwin" && $ARCH = "amd64" ]]; then
curl -sL -o ./cli/modulegen/.__module_gen https://github.com/viamrobotics/module-template-generator/releases/latest/download/macosx_x86_64-main
elif [[ $OS = "linux" && $ARCH = "arm64" ]]; then
curl -sL -o ./cli/modulegen/.__module_gen https://github.com/viamrobotics/module-template-generator/releases/latest/download/linux_aarch64-main
elif [[ $OS = "linux" && $ARCH = "amd64" ]]; then
curl -sL -o ./cli/modulegen/.__module_gen https://github.com/viamrobotics/module-template-generator/releases/latest/download/linux_x86_64-main
fi
Loading