Skip to content
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: release
on:
pull_request:
push:
branches: [ master, test ]
branches: [ master, dev ]
workflow_dispatch:

defaults:
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
version=$new_ver
release="true"
else
version=test-$new_ver\($commit\)
version=dev-$new_ver\($commit\)
sed -E "s|v[0-9]+\.[0-9]+\.[0-9]+|$version|" cmd/compiledb/main.go -i
fi
echo "RELEASE=$release" >> $GITHUB_ENV
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
mv compiledb.txz $out/compiledb-darwin-arm64.txz

# delete draft
gh release delete test --cleanup-tag -y || true
gh release delete dev --cleanup-tag -y || true

ls -la $out

Expand All @@ -100,7 +100,7 @@ jobs:
- name: Update latest
uses: ncipollo/release-action@v1
with:
tag: test
tag: dev
artifactErrorsFailBuild: true
generateReleaseNotes: true
name: ${{ env.VERSION }}
Expand Down
1 change: 1 addition & 0 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set unstable
set shell := ["nu", "-c"]
set script-interpreter := ["nu"]

[private]
default: build

[script]
Expand Down
94 changes: 55 additions & 39 deletions cmd/compiledb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,62 @@ import (

var Version string = "v1.5.3"

func init() {
log.SetOutput(os.Stdout)
// log.SetLevel(log.InfoLevel)
log.SetLevel(log.WarnLevel)
}

func updateConfig(ctx *cli.Context) {
func createConfig(ctx *cli.Context) internal.Config {
outputFile := ctx.String("output")
internal.ParseConfig.InputFile = ctx.String("parse")
internal.ParseConfig.BuildDir = ctx.String("build-dir")
internal.ParseConfig.Exclude = ctx.String("exclude")
internal.ParseConfig.Macros = ctx.String("macros")
internal.ParseConfig.RegexCompile = ctx.String("regex-compile")
internal.ParseConfig.RegexFile = ctx.String("regex-file")
internal.ParseConfig.NoBuild = ctx.Bool("no-build")
internal.ParseConfig.CommandStyle = ctx.Bool("command-style")
internal.ParseConfig.NoStrict = ctx.Bool("no-strict")
internal.ParseConfig.FullPath = ctx.Bool("full-path")

if internal.IsAbsPath(outputFile) == false {
if !internal.IsAbsPath(outputFile) {
cwd, _ := os.Getwd()
outputFile = filepath.Join(cwd, outputFile)
}
internal.ParseConfig.OutputFile = outputFile

if internal.ParseConfig.BuildDir != "" {
err := os.Chdir(internal.ParseConfig.BuildDir)
cfg := internal.Config{
InputFile: ctx.String("parse"),
OutputFile: outputFile,
BuildDir: ctx.String("build-dir"),
Exclude: ctx.String("exclude"),
Macros: ctx.String("macros"),
RegexCompile: ctx.String("regex-compile"),
RegexFile: ctx.String("regex-file"),
NoBuild: ctx.Bool("no-build"),
CommandStyle: ctx.Bool("command-style"),
NoStrict: ctx.Bool("no-strict"),
FullPath: ctx.Bool("full-path"),
}

if cfg.BuildDir != "" {
err := os.Chdir(cfg.BuildDir)
if err != nil {
log.Error(err)
}
}

log.Debugf("Options: %+v", internal.ParseConfig)
return cfg
}

type ActionFunc func(t *internal.Tool, ctx *cli.Context) error

func execute(ctx *cli.Context, fn ActionFunc) error {
logger := log.New()
logger.SetOutput(os.Stdout)

if ctx.Bool("verbose") {
logger.SetLevel(log.DebugLevel)
logger.Info("compiledb-go start, version:", Version)
} else {
logger.SetLevel(log.WarnLevel)
}

cfg := createConfig(ctx)
logger.Debugf("Options: %+v", cfg)

tool := internal.NewTool(cfg, logger)

err := fn(tool, ctx)

if tool.StatusCode != 0 {
os.Exit(tool.StatusCode)
}

return err
}

func newApp() *cli.App {
Expand Down Expand Up @@ -73,20 +96,22 @@ COMMANDS:
"\n\tWhen no subcommand is used it will parse build log/commands and generates" +
"\n\tits corresponding Compilation datAbase.",
Action: func(ctx *cli.Context) error {
updateConfig(ctx)
internal.Generate()
log.Debugf("Done")
return nil
return execute(ctx, func(t *internal.Tool, c *cli.Context) error {
t.Generate()
t.Logger.Debugf("Done")
return nil
})
},
Commands: []*cli.Command{
{
Name: "make",
Usage: "Generates compilation database file for an arbitrary GNU Make...",
SkipFlagParsing: true,
Action: func(ctx *cli.Context) error {
updateConfig(ctx)
internal.MakeWrap(ctx.Args().Slice())
return nil
return execute(ctx, func(t *internal.Tool, c *cli.Context) error {
t.MakeWrap(ctx.Args().Slice())
return nil
})
},
},
},
Expand Down Expand Up @@ -124,11 +149,6 @@ COMMANDS:
Aliases: []string{"v"},
Usage: "Print verbose messages.",
DisableDefaultText: true,
Action: func(*cli.Context, bool) error {
log.SetLevel(log.DebugLevel)
log.Info("compiledb-go start, version:", Version)
return nil
},
},
// &cli.BoolFlag{
// Name: "overwrite",
Expand Down Expand Up @@ -180,8 +200,4 @@ func main() {
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}

if internal.StatusCode != 0 {
os.Exit(internal.StatusCode)
}
}
41 changes: 25 additions & 16 deletions internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"bufio"
"encoding/json"
"os"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

type Config struct {
Expand All @@ -22,18 +21,28 @@ type Config struct {
NoStrict bool
}

var ParseConfig Config
var StatusCode int = 0
type Tool struct {
Config Config
Logger *logrus.Logger
StatusCode int
}

func NewTool(cfg Config, logger *logrus.Logger) *Tool {
return &Tool{
Config: cfg,
Logger: logger,
}
}

func WriteJSON(filename string, cmdCnt int, data *[]Command) {
func (t *Tool) WriteJSON(filename string, cmdCnt int, data *[]Command) {
if cmdCnt == 0 {
return
}

// format
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatalf("Error encoding JSON:%v", err)
t.Logger.Fatalf("Error encoding JSON:%v", err)
}

// write file
Expand All @@ -42,19 +51,19 @@ func WriteJSON(filename string, cmdCnt int, data *[]Command) {
} else {
outfile, err := os.Create(filename)
if err != nil {
log.Fatalf("create %v failed! err:%v", filename, err)
t.Logger.Fatalf("create %v failed! err:%v", filename, err)
}
defer outfile.Close()

_, err = outfile.Write(jsonData)
if err != nil {
log.Fatalf("write %v failed! err:%v", filename, err)
t.Logger.Fatalf("write %v failed! err:%v", filename, err)
}
log.Infof("write %d entries to %s", cmdCnt, filename)
t.Logger.Infof("write %d entries to %s", cmdCnt, filename)
}
}

func Generate() {
func (t *Tool) Generate() {
var (
buildLog []string
scnner *bufio.Scanner
Expand All @@ -63,21 +72,21 @@ func Generate() {
)
defer file.Close()

if ParseConfig.InputFile != "stdin" {
file, err = os.OpenFile(ParseConfig.InputFile, os.O_RDONLY, 0444)
if t.Config.InputFile != "stdin" {
file, err = os.OpenFile(t.Config.InputFile, os.O_RDONLY, 0444)
if err != nil {
log.Fatalf("open %v failed!", ParseConfig.InputFile)
t.Logger.Fatalf("open %v failed!", t.Config.InputFile)
}
scnner = bufio.NewScanner(file)
log.Debugf("Build from file")
t.Logger.Debugf("Build from file")
} else {
scnner = bufio.NewScanner(os.Stdin)
log.Debugf("Build from stdin")
t.Logger.Debugf("Build from stdin")
}

scnner.Buffer(make([]byte, 1024*1024), 1024*1024*100)
for scnner.Scan() {
buildLog = append(buildLog, scnner.Text())
}
Parse(buildLog)
t.Parse(buildLog)
}
23 changes: 11 additions & 12 deletions internal/make_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"os/exec"
"strings"
"sync"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

var makePath = "make"

func MakeWrap(args []string) {
func (t *Tool) MakeWrap(args []string) {
var wg sync.WaitGroup

wg.Add(1)
Expand All @@ -26,25 +25,25 @@ func MakeWrap(args []string) {
cmd.Stderr = &stdoutBuf
cmd.Run()

level := log.GetLevel()
level := t.Logger.GetLevel()

// only print make log
if ParseConfig.NoBuild == false {
log.SetLevel(log.PanicLevel)
if t.Config.NoBuild == false {
t.Logger.SetLevel(logrus.PanicLevel)
}

buildLog := strings.Split(stdoutBuf.String(), "\n")
Parse(buildLog)
t.Parse(buildLog)

// restore log level
if ParseConfig.NoBuild == false {
log.SetLevel(level)
if t.Config.NoBuild == false {
t.Logger.SetLevel(level)
}

wg.Done()
}()

if ParseConfig.NoBuild == false {
if t.Config.NoBuild == false {
cmd := exec.Command(makePath, args...)
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
Expand All @@ -68,8 +67,8 @@ func MakeWrap(args []string) {
go TransferPrintScanner(stderr)

if err := cmd.Wait(); err != nil {
StatusCode = cmd.ProcessState.ExitCode()
fmt.Printf("make failed! errorCode: %d\n", StatusCode)
t.StatusCode = cmd.ProcessState.ExitCode()
fmt.Printf("make failed! errorCode: %d\n", t.StatusCode)
}
}

Expand Down
Loading