diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..94d22ea --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +BINARY=koala +BIN_DIR=$(GOPATH)/bin +PACKAGES=$(shell go list ./... | grep -v /vendor/) + +.PHONY: test +all: $(BIN_DIR) + +$(BIN_DIR): test + go build -v -o $(BIN_DIR)/$(BINARY) ./cmd/$(BINARY) + +test: + $(shell echo "mode: count" > coverage-all.out) + @for pkg in $(PACKAGES); do \ + go test -cover -coverprofile=coverage.out -covermode=count $$pkg; \ + tail -n +2 coverage.out >> ./coverage-all.out; \ + done + +cover: test + go tool cover -html=coverage-all.out + rm -rf coverage-all.out coverage.out + +clean: + rm -rf $(BIN_DIR)/$(BINARY) \ No newline at end of file diff --git a/cmd/koala/main.go b/cmd/koala/main.go index 90d378b..4ba218e 100644 --- a/cmd/koala/main.go +++ b/cmd/koala/main.go @@ -1,104 +1,46 @@ package main import ( - "bufio" - "bytes" - "errors" "flag" - "fmt" "io/ioutil" "log" - "os" - "strings" + + "github.com/guiferpa/koala/file" ) var ( - entrypath string - entryfile string - outpath string - outfile string - target string + entry string + bundled string + tag string ) func init() { - flag.StringVar(&entrypath, "entrypath", ".", "set a custom entrypath for your entryfile") - flag.StringVar(&entryfile, "entryfile", "main", "entryfile is where check lib imports") - flag.StringVar(&outpath, "outpath", "./bin", "set a custom outpath for your distro") - flag.StringVar(&outfile, "outfile", "main", "outfile for export bundle") - flag.StringVar(&target, "target", "import", "set a custom target to mark line that replace content") + flag.StringVar(&entry, "entry", "./entry", "main file to reading") + flag.StringVar(&bundled, "bundled", "./bin/bundled", "output file for export bundle") + flag.StringVar(&tag, "tag", "import", "set a custom tag to mark a target, line that will be replaced by the assign content") } func main() { flag.Parse() - entrypoint := fmt.Sprintf("%s/%s", entrypath, entryfile) - output := fmt.Sprintf("%s/%s", outpath, outfile) - - fileContent, err := ioutil.ReadFile(entrypoint) + payload, err := ioutil.ReadFile(entry) if err != nil { - log.Fatalln(err) + log.Fatal(err) } - originFile := bytes.NewBuffer(fileContent) - copyFile := bytes.NewBufferString(originFile.String()) + currentContent := string(payload) - targetLines, err := getTargetLines(target, copyFile) + targets, err := file.FindOutTarget(tag, currentContent) if err != nil { - panic(err) + log.Fatal(err) } - dstFile, err := replaceByTargetLines(targetLines, originFile.String()) + newestContent, err := file.ReplaceTarget(targets, currentContent) if err != nil { - panic(err) - } - - if _, err := os.Stat(outpath); os.IsNotExist(err) { - os.Mkdir(outpath, 0777) - } - - if err = ioutil.WriteFile(output, dstFile, 0644); err != nil { - panic(err) - } -} - -func getTargetLines(target string, file *bytes.Buffer) ([]string, error) { - if target == "" { - return nil, errors.New("the target is empty") + log.Fatal(err) } - targetLines := make([]string, 0) - scanner := bufio.NewScanner(file) - for scanner.Scan() { - if strings.HasPrefix(scanner.Text(), target) { - claimLibrary := strings.Split(scanner.Text(), " ") - if len(claimLibrary) != 2 { - return nil, errors.New("import syntax wrong") - } - targetLines = append(targetLines, scanner.Text()) - } - } - return targetLines, scanner.Err() -} - -func replaceByTargetLines(targetLines []string, originFile string) ([]byte, error) { - for _, targetLine := range targetLines { - splittedTargetLine := strings.Split(targetLine, " ") - contentLib, err := ioutil.ReadFile(splittedTargetLine[1]) - if err != nil { - return nil, err - } - originFile = strings.Replace(originFile, targetLine, string(contentLib), -1) - } - return []byte(originFile), nil -} -func replaceFileFromImportToSource(fileSource string, libraries []string) (string, error) { - for _, lib := range libraries { - splittedLibraryImport := strings.Split(lib, " ") - libSource, err := ioutil.ReadFile(splittedLibraryImport[1]) - if err != nil { - return "", err - } - fileSource = strings.Replace(fileSource, lib, string(libSource), -1) + if err := file.Build(bundled, newestContent); err != nil { + log.Fatal(err) } - return fileSource, nil } diff --git a/file/file.go b/file/file.go new file mode 100644 index 0000000..21dab1c --- /dev/null +++ b/file/file.go @@ -0,0 +1,32 @@ +package file + +import ( + "os" + "path" +) + +const ( + PermDirectory = 0777 + PermFile = 0644 +) + +func Build(name, s string) error { + dir := path.Dir(name) + if _, err := os.Stat(dir); os.IsNotExist(err) { + if err = os.MkdirAll(dir, PermDirectory); err != nil { + return err + } + return err + } + + file, err := os.Create(name) + if err != nil { + return err + } + defer file.Close() + + file.WriteString(s) + file.Chmod(PermFile) + + return nil +} diff --git a/file/file_test.go b/file/file_test.go new file mode 100644 index 0000000..6f842e8 --- /dev/null +++ b/file/file_test.go @@ -0,0 +1,39 @@ +package file + +import ( + "os" + "path" + "testing" +) + +const base = "./tmp" + +func TestBuildFileIfDirNotExists(t *testing.T) { + name := path.Join(base, "1", "llama.txt") + txt := "testing..." + if err := Build(name, txt); err != nil { + t.Error(err) + } + defer func(d string) { + if err := os.RemoveAll(d); err != nil { + t.Fatal(err) + } + }(base) +} + +func TestBuildFileIfDirAlreadyExists(t *testing.T) { + dir := path.Join(base, "2") + name := path.Join(dir, "koala.txt") + txt := "testing..." + if err := os.MkdirAll(dir, PermDirectory); err != nil { + t.Fatal(err) + } + if err := Build(name, txt); err != nil { + t.Error(err) + } + defer func(d string) { + if err := os.RemoveAll(d); err != nil { + t.Fatal(err) + } + }(base) +} diff --git a/file/target.go b/file/target.go new file mode 100644 index 0000000..22c51e5 --- /dev/null +++ b/file/target.go @@ -0,0 +1,50 @@ +package file + +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io/ioutil" + "strings" +) + +type Target struct { + Tag string + Library string +} + +func (t Target) String() string { + return fmt.Sprintf("%s %s", t.Tag, t.Library) +} + +func FindOutTarget(tag, s string) ([]Target, error) { + targets := make([]Target, 0) + scnr := bufio.NewScanner(bytes.NewBufferString(s)) + for scnr.Scan() { + if strings.HasPrefix(scnr.Text(), tag) { + claimLibrary := strings.Split(scnr.Text(), " ") + if len(claimLibrary) > 2 { + return nil, errors.New("import syntax wrong") + } + if len(claimLibrary) == 2 { + targets = append(targets, Target{ + Tag: claimLibrary[0], + Library: claimLibrary[1], + }) + } + } + } + return targets, nil +} + +func ReplaceTarget(targets []Target, s string) (string, error) { + for _, target := range targets { + library, err := ioutil.ReadFile(target.Library) + if err != nil { + return "", err + } + s = strings.Replace(s, target.String(), string(library), -1) + } + return s, nil +}