gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/engine/strategy_copycheck.go (about)

     1  package engine
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/360EntSecGroup-Skylar/goreporter/linters/copycheck"
     8  	"github.com/360EntSecGroup-Skylar/goreporter/utils"
     9  )
    10  
    11  type StrategyCopyCheck struct {
    12  	Sync *Synchronizer `inject:""`
    13  }
    14  
    15  func (s *StrategyCopyCheck) GetName() string {
    16  	return "CopyCheck"
    17  }
    18  
    19  func (s *StrategyCopyCheck) GetDescription() string {
    20  	return "Query all duplicate code in the project and give duplicate code locations and rows."
    21  }
    22  
    23  func (s *StrategyCopyCheck) GetWeight() float64 {
    24  	return 0.05
    25  }
    26  
    27  // linterCopy provides a function that scans all duplicate code in the project and give
    28  // duplicate code locations and rows.It will extract from the linter need to convert the
    29  // data.The result will be saved in the r's attributes.
    30  func (s *StrategyCopyCheck) Compute(parameters StrategyParameter) (summaries *Summaries) {
    31  	summaries = NewSummaries()
    32  
    33  	copyCodeList := copycheck.CopyCheck(parameters.ProjectPath, parameters.ExceptPackages+",_test.go")
    34  	sumProcessNumber := int64(7)
    35  	processUnit := utils.GetProcessUnit(sumProcessNumber, len(copyCodeList))
    36  
    37  	for i := 0; i < len(copyCodeList); i++ {
    38  		errorSlice := make([]Error, 0)
    39  		for j := 0; j < len(copyCodeList[i]); j++ {
    40  			line := 0
    41  			values := strings.Split(copyCodeList[i][j], ":")
    42  			if len(values) > 1 {
    43  				lines := strings.Split(strings.TrimSpace(values[1]), ",")
    44  				if len(lines) == 2 {
    45  					lineright, _ := strconv.Atoi(lines[1])
    46  					lineleft, _ := strconv.Atoi(lines[0])
    47  					if lineright-lineleft >= 0 {
    48  						line = lineright - lineleft + 1
    49  					}
    50  				}
    51  				values[0] = utils.AbsPath(values[0])
    52  			}
    53  
    54  			errorSlice = append(errorSlice, Error{LineNumber: line, ErrorString: strings.Join(values, ":")})
    55  		}
    56  		summaries.Lock()
    57  		summaries.Summaries[string(i)] = Summary{
    58  			Name:   strconv.Itoa(len(errorSlice)),
    59  			Errors: errorSlice,
    60  		}
    61  		summaries.Unlock()
    62  		if sumProcessNumber > 0 {
    63  			s.Sync.LintersProcessChans <- processUnit
    64  			sumProcessNumber = sumProcessNumber - processUnit
    65  		}
    66  	}
    67  	return
    68  }
    69  
    70  func (s *StrategyCopyCheck) Percentage(summaries *Summaries) float64 {
    71  	summaries.RLock()
    72  	defer summaries.RUnlock()
    73  	return utils.CountPercentage(len(summaries.Summaries))
    74  }