refactor: refactor cmd analysis app

上级 9fcd3b57
......@@ -5,11 +5,12 @@ import (
"fmt"
"github.com/phodal/coca/cmd/cmd_util"
"github.com/phodal/coca/pkg/adapter/cocafile"
"github.com/phodal/coca/pkg/application/analysis/app_concept"
"github.com/phodal/coca/pkg/application/analysis/goapp"
"github.com/phodal/coca/pkg/application/analysis/javaapp"
"github.com/phodal/coca/pkg/application/analysis/pyapp"
"github.com/phodal/coca/pkg/application/analysis/tsapp"
"github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/infrastructure/ast/cocago"
"github.com/spf13/cobra"
"io/ioutil"
)
......@@ -52,58 +53,25 @@ var analysisCmd = &cobra.Command{
}
func AnalysisTypeScript() []core_domain.CodeDataStruct {
importPath := analysisCmdConfig.Path
var results []core_domain.CodeFile
files := cocafile.GetFilesWithFilter(importPath, cocafile.TypeScriptFileFilter)
fmt.Println(files)
for _, file := range files {
fmt.Fprintf(output, "Process TypeScript file: %s\n", file)
app := new(tsapp.TypeScriptApiApp)
content, _ := ioutil.ReadFile(file)
result := app.Analysis(string(content), "")
results = append(results, result)
}
var ds []core_domain.CodeDataStruct
for _, result := range results {
ds = append(ds, result.DataStructures...)
}
return ds
return CommentAnalysis(analysisCmdConfig.Path, new(tsapp.TypeScriptIdentApp), cocafile.TypeScriptFileFilter)
}
func AnalysisPython() []core_domain.CodeDataStruct {
importPath := analysisCmdConfig.Path
var results []core_domain.CodeFile
files := cocafile.GetFilesWithFilter(importPath, cocafile.PythonFileFilter)
for _, file := range files {
fmt.Fprintf(output, "Process Python file: %s\n", file)
app := new(pyapp.PythonApiApp)
content, _ := ioutil.ReadFile(file)
result := app.Analysis(string(content), "")
results = append(results, result)
}
var ds []core_domain.CodeDataStruct
for _, result := range results {
ds = append(ds, result.DataStructures...)
}
return ds
return CommentAnalysis(analysisCmdConfig.Path, new(pyapp.PythonIdentApp), cocafile.PythonFileFilter)
}
func AnalysisGo() []core_domain.CodeDataStruct {
importPath := analysisCmdConfig.Path
return CommentAnalysis(analysisCmdConfig.Path, new(goapp.GoIdentApp), cocafile.GoFileFilter)
}
func CommentAnalysis(path string, app app_concept.AbstractAnalysisApp, filter func(path string) bool) []core_domain.CodeDataStruct {
var results []core_domain.CodeFile
files := cocafile.GetFilesWithFilter(importPath, cocafile.GoFileFilter)
files := cocafile.GetFilesWithFilter(path, filter)
fmt.Println(files)
for _, file := range files {
parser := cocago.NewCocagoParser()
parser.SetOutput(output)
result := parser.ProcessFile(file)
fmt.Fprintf(output, "Process file: %s\n", file)
content, _ := ioutil.ReadFile(file)
result := app.Analysis(string(content), file)
results = append(results, result)
}
......
process file config/cmd_config.go
Process file: config/cmd_config.go
Process Python file: ../_fixtures/grammar/python/blog_entity.py
Process file: ../_fixtures/grammar/python/blog_entity.py
Process TypeScript file: ../_fixtures/grammar/typescript/Class.ts
Process file: ../_fixtures/grammar/typescript/Class.ts
package app_concept
import "github.com/phodal/coca/pkg/domain/core_domain"
type AbstractAnalysisApp interface {
Analysis(code string, path string) core_domain.CodeFile
}
package goapp
import (
"github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/infrastructure/ast/cocago"
)
type GoIdentApp struct {
}
func (g *GoIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
parser := cocago.NewCocagoParser()
return *parser.ProcessString(code, fileName)
}
......@@ -2,6 +2,7 @@ package goapp
import (
. "github.com/onsi/gomega"
"io/ioutil"
"testing"
)
......@@ -9,6 +10,8 @@ func Test_ProcessPackage(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
results := ProcessPackage("../../../../pkg/domain", true)
g.Expect(len(results)).To(Equal(21))
code, _ := ioutil.ReadFile("../../../../pkg/domain/core_domain/code_data_struct.go")
app := &GoIdentApp{}
results := app.Analysis(string(code), "domain")
g.Expect(len(results.DataStructures)).To(Equal(2))
}
\ No newline at end of file
package goapp
import (
"bytes"
"github.com/phodal/coca/pkg/adapter/cocafile"
"github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/infrastructure/ast/cocago"
"strings"
)
func ProcessPackage(path string, debug bool) []*core_domain.CodeFile {
var GoFileFilter = func(path string) bool {
return strings.HasSuffix(path, ".go")
}
files := cocafile.GetFilesWithFilter(path, GoFileFilter)
filesData := make([]*core_domain.CodeFile, len(files))
parser := cocago.NewCocagoParser()
if debug {
buf := new(bytes.Buffer)
parser.SetOutput(buf)
}
for i, file := range files {
processFile := parser.ProcessFile(file)
filesData[i] = &processFile
}
return filesData
}
......@@ -18,10 +18,11 @@ func ProcessTsString(code string) *parser.PythonParser {
return streamToParser(is)
}
type PythonApiApp struct {
type PythonIdentApp struct {
}
func (j *PythonApiApp) Analysis(code string, fileName string) core_domain.CodeFile {
func (j *PythonIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
scriptParser := ProcessTsString(code)
context := scriptParser.Root()
......
......@@ -19,7 +19,7 @@ func Test_AllPythonGrammar(t *testing.T) {
for _, file := range files {
file, _ := ioutil.ReadFile(file)
app := new(PythonApiApp)
app := new(PythonIdentApp)
app.Analysis(string(file), "")
}
......@@ -29,7 +29,7 @@ func Test_AllPythonGrammar(t *testing.T) {
func Test_PythonClassForLexer(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/grammar/class.py")
app.Analysis(string(file), "")
......@@ -40,7 +40,7 @@ func Test_PythonClassForLexer(t *testing.T) {
func Test_PythonArgumentsForLexer(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/grammar/argument.py")
app.Analysis(string(file), "")
......@@ -51,7 +51,7 @@ func Test_PythonArgumentsForLexer(t *testing.T) {
func Test_PythonArgumentsForTryStmt(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/grammar/try_stmt.py")
app.Analysis(string(file), "")
......@@ -62,7 +62,7 @@ func Test_PythonArgumentsForTryStmt(t *testing.T) {
func Test_PythonClassDef(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/grammar/class.py")
codeFile := app.Analysis(string(file), "testdata/grammar/class.py")
......@@ -77,7 +77,7 @@ func Test_PythonClassDef(t *testing.T) {
func Test_PythonClassWithDecorator(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/grammar/class_or_func_def_stmt.py")
codeFile := app.Analysis(string(file), "testdata/grammar/class_or_func_def_stmt.py")
......@@ -92,7 +92,7 @@ func Test_PythonClassWithDecorator(t *testing.T) {
func Test_PythonImport(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/grammar/import_stmt.py")
codeFile := app.Analysis(string(file), "import_stmt")
......@@ -105,7 +105,7 @@ func Test_PythonImport(t *testing.T) {
func Test_PythonClassWithFunctionDef(t *testing.T) {
g := NewGomegaWithT(t)
app := new(PythonApiApp)
app := new(PythonIdentApp)
file, _ := ioutil.ReadFile("testdata/compare/blog_entity.py")
codeFile := app.Analysis(string(file), "testdata/compare/blog_entity.py")
......
......@@ -18,10 +18,11 @@ func ProcessTsString(code string) *parser.TypeScriptParser {
return processStream(is)
}
type TypeScriptApiApp struct {
type TypeScriptIdentApp struct {
}
func (j *TypeScriptApiApp) Analysis(code string, fileName string) core_domain.CodeFile {
func (j *TypeScriptIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
scriptParser := ProcessTsString(code)
context := scriptParser.Program()
......
......@@ -9,7 +9,7 @@ import (
func Test_Regression(t *testing.T) {
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
code, _ := ioutil.ReadFile("../../../../_fixtures/ts/regressions/import_comma_issue.ts")
results := app.Analysis(string(code), "")
......@@ -21,7 +21,7 @@ func Test_Regression(t *testing.T) {
//func Test_ProcessErrorGrammar(t *testing.T) {
// g := NewGomegaWithT(t)
//
// app := new(TypeScriptApiApp)
// app := new(TypeScriptIdentApp)
// code, _ := ioutil.ReadFile("../../../../../_fixtures/ts/regressions/callback_hell.ts")
//
// results := app.Analysis(string(code), "")
......
......@@ -9,7 +9,7 @@ import (
func Test_TypeScriptClassNode(t *testing.T) {
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
codefile := app.Analysis(`
interface IPerson {
name: string;
......@@ -38,7 +38,7 @@ func Test_TypeScriptMultipleClass(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
code, _ := ioutil.ReadFile("../../../../_fixtures/ts/grammar/Class.ts")
codeFile := app.Analysis(string(code), "")
......@@ -52,7 +52,7 @@ func Test_TypeScriptAbstractClass(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
code, _ := ioutil.ReadFile("../../../../_fixtures/ts/grammar/AbstractClass.ts")
codeFile := app.Analysis(string(code), "")
......@@ -67,7 +67,7 @@ func Test_ShouldGetClassFromModule(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
code, _ := ioutil.ReadFile("../../../../_fixtures/ts/grammar/Module.ts")
results := app.Analysis(string(code), "")
......@@ -80,7 +80,7 @@ func Test_ShouldEnableGetClassMethod(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
codefile := app.Analysis(`
class Employee {
......@@ -97,7 +97,7 @@ func Test_ShouldGetInterfaceImplements(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
results := app.Analysis(`
export interface IPerson {
......@@ -121,7 +121,7 @@ func Test_ShouldGetInterfaceProperty(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
codeFile := app.Analysis(`
export interface IPerson {
name: string;
......@@ -148,7 +148,7 @@ func Test_ShouldGetDefaultFunctionName(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
codeFile := app.Analysis(`
function Sum(x: number, y: number) : void {
......@@ -171,7 +171,7 @@ func Test_ShouldHandleRestParameters(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
codeFile := app.Analysis(`
function buildName(firstName: string, ...restOfName: string[]) {
......@@ -190,7 +190,7 @@ func Test_ShouldGetClassFields(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
code, _ := ioutil.ReadFile("../../../../_fixtures/ts/grammar/Class.ts")
codeFile := app.Analysis(string(code), "")
......@@ -204,7 +204,7 @@ func Test_ShouldReturnBlockImports(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
results := app.Analysis(`
import { ZipCodeValidator } from "./ZipCodeValidator";
......@@ -219,7 +219,7 @@ func Test_ShouldReturnAsImports(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
results := app.Analysis(`
import zip = require("./ZipCodeValidator");
......@@ -235,7 +235,7 @@ func Test_ShouldReturnAllImports(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
app := new(TypeScriptIdentApp)
code, _ := ioutil.ReadFile("../../../../_fixtures/ts/grammar/Import.ts")
results := app.Analysis(string(code), "")
......
......@@ -38,15 +38,22 @@ func (n *CocagoParser) ProcessFile(fileName string) core_domain.CodeFile {
fmt.Fprintf(output, "process file %s\n", fileName)
code := string(content)
codeFile := n.ProcessString(code, fileName)
return *codeFile
}
func (n *CocagoParser) ProcessString(code string, fileName string) *core_domain.CodeFile {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fileName, string(content), 0)
f, err := parser.ParseFile(fset, fileName, code, 0)
if err != nil {
panic(err)
}
codeFile := n.Visitor(f, fset, fileName)
currentPackage.CodeFiles = append(currentPackage.CodeFiles, *codeFile)
return *codeFile
return codeFile
}
func (n *CocagoParser) Visitor(f *ast.File, fset *token.FileSet, fileName string) *core_domain.CodeFile {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册