-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create file package to manager the content replace pipeline
- Loading branch information
Showing
5 changed files
with
161 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |