refactor: extract comment generates

上级 63c85126
......@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/phodal/coca/cmd/cmd_util"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/context/analysis"
"github.com/phodal/coca/core/context/tbs"
"github.com/phodal/coca/core/domain"
......@@ -28,7 +28,7 @@ var tbsCmd = &cobra.Command{
Short: "generate tests bad smell",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
files := coca_file.GetJavaTestFiles(tbsCmdConfig.Path)
files := cocafile.GetJavaTestFiles(tbsCmdConfig.Path)
var identifiers []domain.JIdentifier
identifiers = cmd_util.LoadTestIdentify(files)
......
package cocatest
import (
"encoding/json"
"io"
"reflect"
)
// JSONBytesEqual compares the JSON in two byte slices.
func JSONBytesEqual(a, b []byte) (bool, error) {
var j, j2 interface{}
if err := json.Unmarshal(a, &j); err != nil {
return false, err
}
if err := json.Unmarshal(b, &j2); err != nil {
return false, err
}
return reflect.DeepEqual(j2, j), nil
}
func JSONEqual(a, b io.Reader) (bool, error) {
var j, j2 interface{}
d := json.NewDecoder(a)
if err := d.Decode(&j); err != nil {
return false, err
}
d = json.NewDecoder(b)
if err := d.Decode(&j2); err != nil {
return false, err
}
return reflect.DeepEqual(j2, j), nil
}
......@@ -2,13 +2,14 @@ package cocatest
import (
"github.com/phodal/coca/cmd/cmd_util"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/context/analysis"
"github.com/phodal/coca/core/domain"
"path/filepath"
)
func BuildAnalysisResultsByPath(codePath string) (map[string]domain.JIdentifier, []domain.JClassNode) {
files := coca_file.GetJavaTestFiles(codePath)
func BuildTestAnalysisResultsByPath(codePath string) (map[string]domain.JIdentifier, []domain.JClassNode) {
files := cocafile.GetJavaTestFiles(codePath)
var identifiers []domain.JIdentifier
identifiers = cmd_util.LoadTestIdentify(files)
......@@ -23,3 +24,21 @@ func BuildAnalysisResultsByPath(codePath string) (map[string]domain.JIdentifier,
classNodes := analysisApp.AnalysisFiles(identifiers, files, classes)
return identifiersMap, classNodes
}
func BuildAnalysisDeps(codePath string) ([]domain.JClassNode, map[string]domain.JIdentifier) {
codePath = filepath.FromSlash(codePath)
identifierApp := new(analysis.JavaIdentifierApp)
identifiers := identifierApp.AnalysisPath(codePath)
var classes []string = nil
for _, node := range identifiers {
classes = append(classes, node.Package+"."+node.ClassName)
}
callApp := analysis.NewJavaFullApp()
callNodes := callApp.AnalysisPath(codePath, classes, identifiers)
identifiersMap := domain.BuildIdentifierMap(identifiers)
return callNodes, identifiersMap
}
package coca_file
package cocafile
import (
"github.com/antlr/antlr4/runtime/Go/antlr"
......
......@@ -3,7 +3,7 @@ package analysis
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/infrastructure/ast/full"
"path/filepath"
......@@ -17,7 +17,7 @@ func NewJavaFullApp() JavaFullApp {
}
func (j *JavaFullApp) AnalysisPath(codeDir string, classes []string, identNodes []domain.JIdentifier) []domain.JClassNode {
files := coca_file.GetJavaFiles(codeDir)
files := cocafile.GetJavaFiles(codeDir)
return j.AnalysisFiles(identNodes, files, classes)
}
......@@ -33,7 +33,7 @@ func (j *JavaFullApp) AnalysisFiles(identNodes []domain.JIdentifier, files []str
displayName := filepath.Base(file)
fmt.Println("Refactoring parse java call: " + displayName)
parser := coca_file.ProcessFile(file)
parser := cocafile.ProcessFile(file)
context := parser.CompilationUnit()
listener := full.NewJavaFullListener(identMap, file)
......
......@@ -2,7 +2,7 @@ package analysis
import (
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/infrastructure/ast/identifier"
)
......@@ -16,7 +16,7 @@ func NewJavaIdentifierApp() JavaIdentifierApp {
}
func (j *JavaIdentifierApp) AnalysisPath(codeDir string) []domain.JIdentifier {
files := coca_file.GetJavaFiles(codeDir)
files := cocafile.GetJavaFiles(codeDir)
return j.AnalysisFiles(files)
}
......@@ -24,7 +24,7 @@ func (j *JavaIdentifierApp) AnalysisFiles(files []string) []domain.JIdentifier {
var nodeInfos []domain.JIdentifier = nil
for _, file := range files {
parser := coca_file.ProcessFile(file)
parser := cocafile.ProcessFile(file)
context := parser.CompilationUnit()
listener := identifier.NewJavaIdentifierListener()
......
......@@ -3,7 +3,7 @@ package api
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/infrastructure/ast/api"
"path/filepath"
......@@ -15,14 +15,14 @@ type JavaApiApp struct {
}
func (j *JavaApiApp) AnalysisPath(codeDir string, parsedDeps []domain.JClassNode, identifiersMap map[string]domain.JIdentifier, diMap map[string]string) []domain.RestApi {
files := coca_file.GetJavaFiles(codeDir)
files := cocafile.GetJavaFiles(codeDir)
for index := range files {
file := files[index]
displayName := filepath.Base(file)
fmt.Println("Refactoring parse java call: " + displayName)
parser := coca_file.ProcessFile(file)
parser := cocafile.ProcessFile(file)
context := parser.CompilationUnit()
listener := api.NewJavaApiListener(identifiersMap, diMap)
......
......@@ -4,12 +4,9 @@ import (
"encoding/json"
. "github.com/onsi/gomega"
"github.com/phodal/coca/cmd/cmd_util"
"github.com/phodal/coca/core/context/analysis"
"github.com/phodal/coca/cocatest"
"github.com/phodal/coca/core/context/arch/tequila"
"github.com/phodal/coca/core/domain"
"io"
"path/filepath"
"reflect"
"testing"
)
......@@ -17,19 +14,7 @@ func TestConceptAnalyser_Analysis(t *testing.T) {
g := NewGomegaWithT(t)
codePath := "../../../_fixtures/arch/step2-java"
codePath = filepath.FromSlash(codePath)
identifierApp := new(analysis.JavaIdentifierApp)
identifiers := identifierApp.AnalysisPath(codePath)
var classes []string = nil
for _, node := range identifiers {
classes = append(classes, node.Package+"."+node.ClassName)
}
callApp := analysis.NewJavaFullApp()
callNodes := callApp.AnalysisPath(codePath, classes, identifiers)
identifiersMap := domain.BuildIdentifierMap(identifiers)
callNodes, identifiersMap := cocatest.BuildAnalysisDeps(codePath)
app := NewArchApp()
results := app.Analysis(callNodes, identifiersMap)
......@@ -50,26 +35,14 @@ func TestConceptAnalyser_Analysis(t *testing.T) {
jsonContent, _ := json.MarshalIndent(results, "", "\t")
content := cmd_util.ReadFile(filepath.FromSlash(codePath + "/" + "results.json"))
g.Expect(JSONBytesEqual(jsonContent, content)).To(Equal(true))
g.Expect(cocatest.JSONBytesEqual(jsonContent, content)).To(Equal(true))
}
func TestConceptAnalyser_AnalysisWithFans(t *testing.T) {
g := NewGomegaWithT(t)
codePath := "../../../_fixtures/arch/step2-java"
codePath = filepath.FromSlash(codePath)
identifierApp := new(analysis.JavaIdentifierApp)
identifiers := identifierApp.AnalysisPath(codePath)
var classes []string = nil
for _, node := range identifiers {
classes = append(classes, node.Package+"."+node.ClassName)
}
callApp := analysis.NewJavaFullApp()
callNodes := callApp.AnalysisPath(codePath, classes, identifiers)
identifiersMap := domain.BuildIdentifierMap(identifiers)
callNodes, identifiersMap := cocatest.BuildAnalysisDeps(codePath)
app := NewArchApp()
result := app.Analysis(callNodes, identifiersMap)
......@@ -87,28 +60,3 @@ func TestConceptAnalyser_AnalysisWithFans(t *testing.T) {
g.Eventually(fans[0].FanIn).Should(Equal(2))
g.Eventually(fans[0].FanOut).Should(Equal(0))
}
func JSONEqual(a, b io.Reader) (bool, error) {
var j, j2 interface{}
d := json.NewDecoder(a)
if err := d.Decode(&j); err != nil {
return false, err
}
d = json.NewDecoder(b)
if err := d.Decode(&j2); err != nil {
return false, err
}
return reflect.DeepEqual(j2, j), nil
}
// JSONBytesEqual compares the JSON in two byte slices.
func JSONBytesEqual(a, b []byte) (bool, error) {
var j, j2 interface{}
if err := json.Unmarshal(a, &j); err != nil {
return false, err
}
if err := json.Unmarshal(b, &j2); err != nil {
return false, err
}
return reflect.DeepEqual(j2, j), nil
}
......@@ -3,7 +3,7 @@ package bs
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/domain/bs_domain"
"github.com/phodal/coca/core/infrastructure/ast/bs"
"path/filepath"
......@@ -20,7 +20,7 @@ func NewBadSmellApp() *BadSmellApp {
func (j *BadSmellApp) AnalysisPath(codeDir string) *[]bs_domain.BsJClass {
nodeInfos = nil
files := coca_file.GetJavaFiles(codeDir)
files := cocafile.GetJavaFiles(codeDir)
for index := range files {
nodeInfo := bs_domain.NewJFullClassNode()
file := files[index]
......@@ -28,7 +28,7 @@ func (j *BadSmellApp) AnalysisPath(codeDir string) *[]bs_domain.BsJClass {
displayName := filepath.Base(file)
fmt.Println("Refactoring parse java call: " + displayName)
parser := coca_file.ProcessFile(file)
parser := cocafile.ProcessFile(file)
context := parser.CompilationUnit()
listener := bs.NewBadSmellListener()
......
......@@ -22,8 +22,8 @@ func TestAnalysis(t *testing.T) {
func Test_ShouldCountDeps_WhenHadClassNodes(t *testing.T) {
g := NewGomegaWithT(t)
codePath := "../../../_fixtures/examples/api"
_, classNodes := cocatest.BuildAnalysisResultsByPath(codePath)
codePath := "../../../_fixtures/examples/api/"
_, classNodes := cocatest.BuildTestAnalysisResultsByPath(codePath)
depApp := NewDepApp()
depApp.CountDeps(classNodes)
......
......@@ -4,7 +4,7 @@ import (
"bufio"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
base2 "github.com/phodal/coca/core/context/refactor/base"
models2 "github.com/phodal/coca/core/context/refactor/base/models"
"io"
......@@ -34,13 +34,13 @@ func NewMoveClassApp(config string, pPath string) *MoveClassApp {
func (j *MoveClassApp) Analysis() []models2.JMoveStruct {
// TODO: 使用 Deps.json 来移动包
files := coca_file.GetJavaFiles(configPath)
files := cocafile.GetJavaFiles(configPath)
for index := range files {
file := files[index]
currentFile, _ = filepath.Abs(file)
parser := coca_file.ProcessFile(file)
parser := cocafile.ProcessFile(file)
context := parser.CompilationUnit()
node := models2.NewJFullIdentifier()
......
......@@ -3,7 +3,7 @@ package unused
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
base2 "github.com/phodal/coca/core/context/refactor/base"
models2 "github.com/phodal/coca/core/context/refactor/base/models"
"io/ioutil"
......@@ -25,7 +25,7 @@ func NewRemoveUnusedImportApp(pPath string) *RemoveUnusedImportApp {
}
func (j *RemoveUnusedImportApp) Analysis() []models2.JFullIdentifier {
files := coca_file.GetJavaFiles(configPath)
files := cocafile.GetJavaFiles(configPath)
var nodes []models2.JFullIdentifier = nil
for index := range files {
......@@ -35,7 +35,7 @@ func (j *RemoveUnusedImportApp) Analysis() []models2.JFullIdentifier {
displayName := filepath.Base(file)
fmt.Println("Refactoring parse java call: " + displayName)
parser := coca_file.ProcessFile(file)
parser := cocafile.ProcessFile(file)
context := parser.CompilationUnit()
node := models2.NewJFullIdentifier()
......
......@@ -129,7 +129,7 @@ func TestTbsApp_ShouldReturnMultipleResult(t *testing.T) {
}
func buildTbsResult(codePath string) []TestBadSmell {
identifiersMap, classNodes := cocatest.BuildAnalysisResultsByPath(codePath)
identifiersMap, classNodes := cocatest.BuildTestAnalysisResultsByPath(codePath)
app := NewTbsApp()
result := app.AnalysisPath(classNodes, identifiersMap)
......
......@@ -3,7 +3,7 @@ package todo
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/adapter/cocafile"
"github.com/phodal/coca/core/adapter/shell"
"github.com/phodal/coca/core/context/git"
"github.com/phodal/coca/core/context/todo/astitodo"
......@@ -64,7 +64,7 @@ func (a TodoApp) BuildWithGitHistory(todos []*astitodo.TODO) []TodoDetail {
func buildComments(path string) []*astitodo.TODO {
var todos []*astitodo.TODO
files := coca_file.GetJavaFiles(path)
files := cocafile.GetJavaFiles(path)
for index := range files {
file := files[index]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册