Skip to content

Commit

Permalink
GlobalFee, gorelease, rename cli, comment swaps, multiline delete (#11)
Browse files Browse the repository at this point in the history
* showcase features to disable

* add go releaser for Linux, Mac, and Windows

* initial: add github.com/reecepbcups/globalfee

* rm globalfee, multiline spawntag's, uncomment tags

* cli!: `new` - CommetSwap, multi line fixes

* generalized test_node sh script + replacements
  • Loading branch information
Reecepbcups authored Feb 9, 2024
1 parent d9b2508 commit fc5c322
Show file tree
Hide file tree
Showing 11 changed files with 463 additions and 15 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: "Release"

on:
push:
tags:
- '**'

# Test Locally with:
# goreleaser build --skip-validate --config gorelease.yaml --snapshot

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2.3.4
with:
fetch-depth: 0

- uses: actions/setup-go@v2
with:
go-version: '1.21'

- name: Clean up dist directory
run: rm -rf dist

- name: Build
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: build --skip-validate --config gorelease.yaml

- name: Release
uses: goreleaser/goreleaser-action@v5
if: startsWith(github.ref, 'refs/tags/')
with:
version: latest
args: release --skip-validate --clean
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local-ic
heighliner/
dist/

my-project/
my-project/
112 changes: 101 additions & 11 deletions cmd/spawn/new-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type SpawnNewConfig struct {
AppName string
AppDirName string
BinaryName string
TokenDenom string

IgnoreFiles []string

Expand All @@ -29,24 +30,29 @@ const (
FlagWalletPrefix = "bech32"
FlagBinaryName = "bin"
FlagDebugging = "debug"
FlagTokenDenom = "denom"

FlagDisabled = "disabled"
FlagDisabled = "disable"
)

var IgnoredFiles = []string{"generate.sh", "embed.go"}
var (
IgnoredFiles = []string{"generate.sh", "embed.go"}
SupportedFeatures = []string{"tokenfactory", "poa", "globalfee", "wasm", "ibc", "nft", "group", "circuit"}
)

func init() {
newChain.Flags().String(FlagWalletPrefix, "cosmos", "chain wallet bech32 prefix")
newChain.Flags().String(FlagBinaryName, "appd", "binary name")
newChain.Flags().Bool(FlagDebugging, false, "enable debugging")
newChain.Flags().StringSlice(FlagDisabled, []string{}, "disable features")
newChain.Flags().StringSlice(FlagDisabled, []string{}, "disable features: "+strings.Join(SupportedFeatures, ", "))
newChain.Flags().String(FlagTokenDenom, "stake", "token denom")
}

// TODO: reduce required inputs here. (or make them flags with defaults?)
var newChain = &cobra.Command{
Use: "new-chain [project-name]",
Use: "new [project-name]",
Short: "List all current chains or outputs a current config information",
Example: fmt.Sprintf(`spawn new-chain my-project --%s=cosmos --%s=appd`, FlagWalletPrefix, FlagBinaryName),
Example: fmt.Sprintf(`spawn new project --%s=cosmos --%s=appd`, FlagWalletPrefix, FlagBinaryName),
Args: cobra.ExactArgs(1),
// ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// return GetFiles(), cobra.ShellCompDirectiveNoFileComp
Expand All @@ -57,6 +63,7 @@ var newChain = &cobra.Command{

walletPrefix, _ := cmd.Flags().GetString(FlagWalletPrefix)
binName, _ := cmd.Flags().GetString(FlagBinaryName)
denom, _ := cmd.Flags().GetString(FlagTokenDenom)

debug, _ := cmd.Flags().GetBool(FlagDebugging)

Expand All @@ -68,8 +75,8 @@ var newChain = &cobra.Command{
AppName: appName,
AppDirName: "." + projName,
BinaryName: binName,

Debugging: debug,
TokenDenom: denom,
Debugging: debug,

// by default everything is on, then we remove what the user wants to disable
DisabledFeatures: disabled,
Expand Down Expand Up @@ -138,6 +145,11 @@ func NewChain(cfg SpawnNewConfig) {
return nil
}

if relPath == "scripts/test_node.sh" {
fc = strings.ReplaceAll(fc, "export BINARY=${BINARY:-wasmd}", fmt.Sprintf("export BINARY=${BINARY:-%s}", binaryName))
fc = strings.ReplaceAll(fc, "export DENOM=${DENOM:-token}", fmt.Sprintf("export DENOM=${DENOM:-%s}", cfg.TokenDenom))
}

// TODO: regex would be nicer for replacing incase it changes up stream. may never though. Also limit to specific files?
fc = strings.ReplaceAll(fc, ".wasmd", appDirName)
fc = strings.ReplaceAll(fc, `const appName = "WasmApp"`, fmt.Sprintf(`const appName = "%s"`, appName))
Expand Down Expand Up @@ -186,6 +198,8 @@ func removeDisabledFeatures(disabled []string, relativePath string, fileContent
fileContent = removeTokenFactory(relativePath, fileContent)
case "poa":
fileContent = removePoa(relativePath, fileContent)
case "globalfee":
fileContent = removeGlobalFee(relativePath, fileContent)
case "ibc": // this would remove all. Including PFM, then we can have others for specifics (i.e. ICAHost, IBCFees)
// fileContent = removeIbc(relativePath, fileContent)
continue
Expand Down Expand Up @@ -217,6 +231,10 @@ func removeTokenFactory(relativePath string, fileContent []byte) []byte {
fileContent = RemoveGeneralModule("tokenfactory", string(fileContent))
}

if relativePath == "scripts/test_node.sh" {
fileContent = RemoveGeneralModule("tokenfactory", string(fileContent))
}

return fileContent
}

Expand All @@ -229,6 +247,31 @@ func removePoa(relativePath string, fileContent []byte) []byte {
fileContent = RemoveGeneralModule("poa", string(fileContent))
}

if relativePath == "scripts/test_node.sh" {
fileContent = RemoveGeneralModule("poa", string(fileContent))
}

return fileContent
}

func removeGlobalFee(relativePath string, fileContent []byte) []byte {

fileContent = HandleCommentSwaps("globalfee", string(fileContent))
fileContent = RemoveTaggedLines("globalfee", string(fileContent), true)

if relativePath == "go.mod" || relativePath == "go.sum" {
fileContent = RemoveGoModImport("github.com/reecepbcups/globalfee", fileContent)
}

if relativePath == "app/app.go" || relativePath == "app/ante.go" {
fileContent = RemoveGeneralModule("globalfee", string(fileContent))
fileContent = RemoveGeneralModule("GlobalFee", string(fileContent))
}

if relativePath == "scripts/test_node.sh" {
fileContent = RemoveGeneralModule("globalfee", string(fileContent))
}

return fileContent
}

Expand Down Expand Up @@ -302,20 +345,67 @@ func removeWasm(relativePath string, fileContent []byte) []byte {
return fileContent
}

// Sometimes if we remove a module, we want to delete one line and use another.
func HandleCommentSwaps(name string, fileContent string) []byte {
newContent := make([]string, 0, len(strings.Split(fileContent, "\n")))

uncomment := fmt.Sprintf("?spawntag:%s", name)

for idx, line := range strings.Split(fileContent, "\n") {
hasUncommentTag := strings.Contains(line, uncomment)
if hasUncommentTag {
line = strings.Replace(line, "//", "", 1)
line = strings.TrimRight(strings.Replace(line, fmt.Sprintf("// %s", uncomment), "", 1), " ")
fmt.Printf("uncomment %s: %d, %s\n", name, idx, line)
}

newContent = append(newContent, line)
}

return []byte(strings.Join(newContent, "\n"))
}

const expectedFormat = "// spawntag:"

// RemoveTaggedLines deletes tagged lines or just removes the comment if desired.
func RemoveTaggedLines(name string, fileContent string, delete bool) []byte {
func RemoveTaggedLines(name string, fileContent string, deleteLine bool) []byte {
newContent := make([]string, 0, len(strings.Split(fileContent, "\n")))

for _, line := range strings.Split(fileContent, "\n") {
startIdx := -1
for idx, line := range strings.Split(fileContent, "\n") {
// TODO: regex anything in between // and spawntag such as spaces, symbols, etc.
line = strings.ReplaceAll(line, "//spawntag:", expectedFormat) // just QOL for us to not tear our hair out

hasTag := strings.Contains(line, fmt.Sprintf("spawntag:%s", name))
hasMultiLineTag := strings.Contains(line, fmt.Sprintf("!spawntag:%s", name))

// if the line has a tag, and the tag starts with a !, then we will continue until we find the end of the tag with another.
if startIdx != -1 {
if !hasMultiLineTag {
continue
}

startIdx = -1
fmt.Println("endIdx:", idx, line)
continue
}

if hasMultiLineTag {
if !deleteLine {
continue
}

startIdx = idx
fmt.Printf("startIdx %s: %d, %s\n", name, idx, line)
continue
}

if hasTag {
if delete {
if deleteLine {
continue
}

line = strings.Split(line, "// spawntag:")[0]
line = strings.Split(line, expectedFormat)[0]
line = strings.TrimRight(line, " ")
}

Expand Down
110 changes: 110 additions & 0 deletions gorelease.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
project_name: spawn
release:
github:
owner: strangelove-ventures
name: spawn
name_template: '{{.Tag}}'
builds:
- id: spawn
goos:
- linux
- darwin
- windows
goarch:
- amd64
- arm64
- "386"
goarm:
- "6"
gomips:
- hardfloat
goamd64:
- v1
targets:
- linux_amd64_v1
- linux_arm64
- linux_386
- darwin_amd64_v1
- darwin_arm64
- windows_amd64_v1
- windows_arm64
- windows_386
dir: ./cmd/spawn
main: .
binary: spawn
builder: go
gobinary: go
command: build
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
archives:
- id: default
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}'
format: tar.gz
files:
- src: license*
- src: LICENSE*
- src: readme*
- src: README*
- src: changelog*
- src: CHANGELOG*
snapshot:
name_template: '{{ .Version }}-SNAPSHOT-{{ .ShortCommit }}'
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt'
algorithm: sha256
dist: dist
env_files:
github_token: ~/.config/goreleaser/github_token
gitlab_token: ~/.config/goreleaser/gitlab_token
gitea_token: ~/.config/goreleaser/gitea_token
source:
name_template: '{{ .ProjectName }}-{{ .Version }}'
format: tar.gz
gomod:
gobinary: go
announce:
twitter:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
mastodon:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
server: ""
reddit:
title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
url_template: '{{ .ReleaseURL }}'
slack:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
username: GoReleaser
discord:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
author: GoReleaser
color: "3888754"
icon_url: https://goreleaser.com/static/avatar.png
teams:
title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
color: '#2D313E'
icon_url: https://goreleaser.com/static/avatar.png
smtp:
subject_template: '{{ .ProjectName }} {{ .Tag }} is out!'
body_template: 'You can view details from: {{ .ReleaseURL }}'
mattermost:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
username: GoReleaser
linkedin:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
telegram:
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
webhook:
message_template: '{ "message": "{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}"}'
content_type: application/json; charset=utf-8
opencollective:
title_template: '{{ .Tag }}'
message_template: '{{ .ProjectName }} {{ .Tag }} is out!<br/>Check it out at <a href="{{ .ReleaseURL }}">{{ .ReleaseURL }}</a>'
git:
tag_sort: -version:refname
github_urls:
download: https://github.com
gitlab_urls:
download: https://gitlab.com
2 changes: 1 addition & 1 deletion scripts/useful_scripts.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

# Spawn a new instance
make install && spawn new-chain my-project --debug --bech32=cosmos --bin=appd --disabled=tokenfactory
make install && spawn new my-project --debug --bech32=cosmos --bin=appd --disable=tokenfactory
Loading

0 comments on commit fc5c322

Please sign in to comment.