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

feat: translate bills in a single web page by wasm #77

Merged
merged 17 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

.vscode/
bin/
wasm-dist/

# Alipay records
alipay_records_*.csv
Expand Down
42 changes: 39 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ GOPATH ?= $(shell go env GOPATH)
BIN_DIR := $(GOPATH)/bin
GOLANGCI_LINT := $(BIN_DIR)/golangci-lint

# https://stackoverflow.com/a/14777895
ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
detected_OS := Windows
else
detected_OS := $(shell uname) # same as "uname -s"
endif

ifeq ($(detected_OS), Windows)
BROWSER := start
else
ifeq (,$(findstring Drawin,$(detected_OS))) # Mac OS X
BROWSER := open
else # consider as Linux or others
BROWSER := xdg-open
endif
endif

# All targets.
.PHONY: lint test build container push help clean test-go test-wechat test-alipay test-huobi test-htsec format check-format goreleaser-build-test

Expand All @@ -50,12 +67,15 @@ help: ## Display this help

build: build-local ## Build the project

LD_FLAGS := -ldflags "-s -w
LD_FLAGS += -X $(ROOT)/pkg/version.VERSION=$(VERSION)
LD_FLAGS += -X $(ROOT)/pkg/version.REPOROOT=$(ROOT)
LD_FLAGS += -X $(ROOT)/pkg/version.COMMIT=$(GIT_COMMIT)"

build-local:
@for target in $(TARGETS); do \
go build -v -o $(OUTPUT_DIR)/$${target} \
-ldflags "-s -w -X $(ROOT)/pkg/version.VERSION=$(VERSION) \
-X $(ROOT)/pkg/version.REPOROOT=$(ROOT) \
-X $(ROOT)/pkg/version.COMMIT=$(GIT_COMMIT)" \
$(LD_FLAGS) \
$(CMD_DIR)/; \
done

Expand All @@ -68,6 +88,7 @@ clean: ## Clean all the temporary files
@rm -rf ./dist
@rm -rf ./test/output
@rm -rf ./double-entry-generator
@rm -rf ./wasm-dist

test: test-go test-alipay test-wechat test-huobi test-htsec ## Run all tests

Expand All @@ -94,3 +115,18 @@ check-format: ## Check if the format looks good.

goreleaser-build-test: ## Goreleaser build for testing
goreleaser build --single-target --snapshot --rm-dist

clean-wasm: ## Clean wasm-dist dir
@rm -rf ./wasm-dist

build-wasm: clean-wasm ## Build WebAssembly's version
@mkdir -p wasm-dist
GOOS=js GOARCH=wasm go build -o wasm-dist/double-entry-generator.wasm $(LD_FLAGS) $(CMD_DIR)/
@cp "$$(go env GOROOT)/misc/wasm/wasm_exec.js" wasm-dist/
@cp wasm/* wasm-dist/
@echo "Build wasm completed! Type \`make run-wasm-server\` to run wasm."

run-wasm-server: ## Run WebAssembly in browser
@cd wasm-dist
# @$(BROWSER) http://127.0.0.1:2000
@python -m http.server --directory wasm-dist --bind 127.0.0.1 2000
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.12
require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/spf13/viper v1.14.0
github.com/xuri/excelize/v2 v2.5.0
golang.org/x/text v0.3.6
golang.org/x/text v0.4.0
)
1,057 changes: 1,002 additions & 55 deletions go.sum

Large diffs are not rendered by default.

26 changes: 2 additions & 24 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/deb-sig/double-entry-generator/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string
Expand Down Expand Up @@ -65,26 +64,5 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".double-entry-generator" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".double-entry-generator")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
config.InitConfig(cfgFile)
}
19 changes: 3 additions & 16 deletions pkg/cmd/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ limitations under the License.
package cmd

import (
"fmt"
"log"
"os"

"github.com/deb-sig/double-entry-generator/pkg/cmd/validator"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Triple-Z marked this conversation as resolved.
Show resolved Hide resolved

"github.com/deb-sig/double-entry-generator/pkg/compiler"
"github.com/deb-sig/double-entry-generator/pkg/config"
Expand All @@ -42,18 +40,7 @@ var translateCmd = &cobra.Command{
Short: "Translate the bills to a given format",
Long: ``,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("Failed to translate: Require the bill file")
} else if len(args) > 1 {
// TODO(gaocegege): support it.
return fmt.Errorf("Failed to translate: Do not support multi-file now")
}

_, err := os.Stat(args[0])
if err == nil {
return nil
}
return fmt.Errorf("Failed to translate: %v", err)
return validator.TranslateArgs(args)
},
Run: func(cmd *cobra.Command, args []string) {
run(args)
Expand Down
24 changes: 24 additions & 0 deletions pkg/cmd/validator/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !js
// +build !js

package validator

import (
"fmt"
"os"
)

func TranslateArgs(args []string) error {
if len(args) < 1 {
return fmt.Errorf("Failed to translate: Require the bill file")
} else if len(args) > 1 {
// TODO(gaocegege): support it.
return fmt.Errorf("Failed to translate: Do not support multi-file now")
}

_, err := os.Stat(args[0])
if err == nil {
return nil
}
return fmt.Errorf("Failed to translate: %v", err)
}
13 changes: 13 additions & 0 deletions pkg/cmd/validator/validator_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build js && wasm
// +build js,wasm

package validator

import "fmt"

func TranslateArgs(args []string) error {
if len(args) < 1 {
return fmt.Errorf("failed to translate: Require the bill content")
}
return nil
}
24 changes: 14 additions & 10 deletions pkg/compiler/beancount/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"
"io"
"log"
"os"
"sort"
"text/template"

"github.com/deb-sig/double-entry-generator/pkg/analyser"
"github.com/deb-sig/double-entry-generator/pkg/config"
"github.com/deb-sig/double-entry-generator/pkg/io/writer"
"github.com/deb-sig/double-entry-generator/pkg/ir"
"github.com/deb-sig/double-entry-generator/pkg/util"
)
Expand Down Expand Up @@ -94,25 +94,29 @@ func (b *BeanCount) Compile() error {
b.IR.Orders[index].Tags = tags
}

log.Printf("Writing to %s", b.Output)
file, err := os.Create(b.Output)
outputWriter, err := writer.GetWriter(b.Output)
if err != nil {
return fmt.Errorf("create output file %s error: %v", b.Output, err)
return fmt.Errorf("can't get output writer, err: %v", err)
}
defer file.Close()
defer func(outputWriter writer.OutputWriter) {
err := outputWriter.Close()
if err != nil {
log.Printf("output writer close err: %v\n", err)
}
}(outputWriter)

if !b.AppendMode {
if err := b.writeHeader(file); err != nil {
if err := b.writeHeader(outputWriter); err != nil {
return err
}
}

log.Printf("Finished to write to %s", b.Output)
return b.writeBills(file)
return b.writeBills(outputWriter)
}

// writeHeader writes the acounts and title into the file.
func (b *BeanCount) writeHeader(file *os.File) error {
func (b *BeanCount) writeHeader(file io.Writer) error {
_, err := io.WriteString(file, "option \"title\" \""+b.Config.Title+"\"\n")
if err != nil {
return fmt.Errorf("write option title error: %v", err)
Expand Down Expand Up @@ -146,7 +150,7 @@ func (b *BeanCount) writeHeader(file *os.File) error {
}

// writeBills writes bills to the file.
func (b *BeanCount) writeBills(file *os.File) error {
func (b *BeanCount) writeBills(file io.Writer) error {
// Sort the bills from earliest to lastest.
// If the bills are the same day, the tx which has lower
// line number is considered happened earlier than the tx
Expand All @@ -163,7 +167,7 @@ func (b *BeanCount) writeBills(file *os.File) error {
return nil
}

func (b *BeanCount) writeBill(file *os.File, index int) error {
func (b *BeanCount) writeBill(file io.Writer, index int) error {
o := b.IR.Orders[index]

var buf bytes.Buffer
Expand Down
37 changes: 37 additions & 0 deletions pkg/config/init_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build !js
// +build !js

package config

import (
"fmt"
"os"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

func InitConfig(cfgFile string) {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".double-entry-generator" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".double-entry-generator")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
21 changes: 21 additions & 0 deletions pkg/config/init_config_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build js && wasm
// +build js,wasm

package config

import (
"bytes"
"fmt"
"github.com/spf13/viper"
Triple-Z marked this conversation as resolved.
Show resolved Hide resolved
)

func InitConfig(cfgContent string) {
if cfgContent != "" {
viper.AutomaticEnv()
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer([]byte(cfgContent)))
} else {
fmt.Errorf("[ERROR] Can't get config from args!")
}
return
}
29 changes: 29 additions & 0 deletions pkg/io/reader/file_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !js
// +build !js

package reader

import (
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"os"
Triple-Z marked this conversation as resolved.
Show resolved Hide resolved
)

func GetReader(filename string) (io.Reader, error) {
csvFile, err := os.Open(filename)
if err != nil {
return nil, err
}
return csvFile, nil
}

func GetGBKReader(filename string) (io.Reader, error) {
csvFile, err := GetReader(filename)
if err != nil {
return nil, err
}

gbkReader := transform.NewReader(csvFile, simplifiedchinese.GBK.NewDecoder())
return gbkReader, nil
}
19 changes: 19 additions & 0 deletions pkg/io/reader/wasm_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build js && wasm
// +build js,wasm

package reader

import (
"bufio"
"bytes"
"io"
)

func GetReader(fileContent string) (io.Reader, error) {
return bufio.NewReader(bytes.NewBuffer([]byte(fileContent))), nil
}

// GetGBKReader is for alipay provider, at WASM is same as GetReader
func GetGBKReader(fileContent string) (io.Reader, error) {
return GetReader(fileContent)
}
19 changes: 19 additions & 0 deletions pkg/io/writer/file_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !js
// +build !js

package writer

import (
"fmt"
"log"
"os"
)

func GetWriter(outputFile string) (OutputWriter, error) {
log.Printf("Writing to %s", outputFile)
file, err := os.Create(outputFile)
if err != nil {
return nil, fmt.Errorf("create output file %s error: %v", outputFile, err)
}
return file, nil
}
Loading