|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "alipay.com/code_insight/coref-go-extractor/src/config" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "alipay.com/code_insight/coref-go-extractor/src/util" |
| 11 | +) |
| 12 | + |
| 13 | +func usage() { |
| 14 | + fmt.Fprintf(os.Stderr, |
| 15 | + `%s is a wrapper script that installs dependencies and calls the extractor. |
| 16 | +In resource-constrained environments, the environment variable SPARROW_EXTRACTOR_GO_MAX_GOROUTINES |
| 17 | +(or its legacy alias MAX_GOROUTINES) can be used to limit the number of parallel goroutines |
| 18 | +started by the extractor, which reduces CPU and memory requirements. The default value for this |
| 19 | +variable is CPU Nums. |
| 20 | +The extractor for go tests files<*_test.go | *.test> is extracted by default. |
| 21 | +If you don't need extract go tests files, please add extraction flag with ' -ex noneedtests'; |
| 22 | +however, types' info of target dependencies is not extracted by default, if you want do extraction for |
| 23 | +that, use flag with '-ex needdependency', see the demo below. |
| 24 | +`, os.Args[0]) |
| 25 | + fmt.Fprintf(os.Stderr, "Usage:\n\n %s [<flag for extraction>...] [<buildflag For go>...] [--] <fileRoot>\n", os.Args[0]) |
| 26 | + fmt.Fprintf(os.Stderr, " Example#1: %s -o ./out/coref_go_src.db $src_root \n", os.Args[0]) |
| 27 | + fmt.Fprintf(os.Stderr, " Example#2: %s -o ./out/coref_go_src.db -ex noneedtests -ex needscope -mod=mod $src_root \n", os.Args[0]) |
| 28 | +} |
| 29 | + |
| 30 | +type extractionConf struct { |
| 31 | + extractor string |
| 32 | + extractArgs []string |
| 33 | + buildArgs []string |
| 34 | + dbPath string |
| 35 | + dbName string |
| 36 | + srcRoot string |
| 37 | + shouldInstallDependency bool |
| 38 | +} |
| 39 | + |
| 40 | +// DependencyInstallerMode is an enum describing how dependencies should be installed |
| 41 | +type DependencyInstallerMode int |
| 42 | + |
| 43 | +const ( |
| 44 | + // GoGetNoModules represents dependency installation using `go get` without modules |
| 45 | + GoGetNoModules DependencyInstallerMode = iota |
| 46 | + // GoGetWithModules represents dependency installation using `go get` with modules |
| 47 | + GoGetWithModules |
| 48 | +) |
| 49 | + |
| 50 | +type GetMode int |
| 51 | + |
| 52 | +const ( |
| 53 | + ModUnset GetMode = iota |
| 54 | + ModGet |
| 55 | + ModMod |
| 56 | + ModVendor |
| 57 | +) |
| 58 | + |
| 59 | +func main() { |
| 60 | + startTime := time.Now() |
| 61 | + |
| 62 | + // os.Args |
| 63 | + cfg := parseExtractorRunningParams([]string{"./extractor_cli", "-o", "./out/coref_go_src.db", "-ex", "needmodfile", "-ex", "noneedtests", "."}) |
| 64 | + log.Printf("ExtractorRunningParams: %+v\n", cfg) |
| 65 | + |
| 66 | + currentDir, err := os.Getwd() |
| 67 | + if err != nil { |
| 68 | + log.Fatalf("Failed to get current directory: %s", err) |
| 69 | + } |
| 70 | + |
| 71 | + // setting up for go env |
| 72 | + util.PrepareRunEnv() |
| 73 | + |
| 74 | + // should load project from extraction src root |
| 75 | + if err := os.Chdir(cfg.srcRoot); err != nil { |
| 76 | + log.Fatalf("Failed to change directory to the extraction source root: %s", err) |
| 77 | + } |
| 78 | + |
| 79 | + if cfg.shouldInstallDependency { |
| 80 | + handleDependencyInstallation(currentDir) |
| 81 | + } |
| 82 | + |
| 83 | + // for compatible with go111 |
| 84 | + // see detail @https://maelvls.dev/go111module-everywhere/ |
| 85 | + // disable go mod Load by default. |
| 86 | + if err := os.Setenv("GO111MODULE", "off"); err != nil { |
| 87 | + log.Fatalf("Failed to set GO111MODULE environment variable: %s", err) |
| 88 | + } |
| 89 | + |
| 90 | + log.Printf("Done preparing workspace in %v.\n", time.Since(startTime)) |
| 91 | + log.Printf("Running extractor '%s' from directory '%s'.\n", cfg.extractor, cfg.srcRoot) |
| 92 | + |
| 93 | + runGoExtraction(cfg) |
| 94 | +} |
| 95 | + |
| 96 | +func setExtractorBasicConfig(cfg *extractionConf) *config.Config { |
| 97 | + // Initialize the basic configuration with default values |
| 98 | + basicConfig := &config.Config{ |
| 99 | + Store: &config.OrmDesc{ |
| 100 | + DBDialect: "sqlite3", |
| 101 | + DBName: "coref_go_src.db", |
| 102 | + DBPath: ".", |
| 103 | + }, |
| 104 | + ParseRule: &config.ParseConfig{ |
| 105 | + NeedTests: true, // Default to including tests |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + // Update the database path and name if provided in the cfg |
| 110 | + if cfg.dbPath != "" { |
| 111 | + basicConfig.Store.DBPath = cfg.dbPath |
| 112 | + } |
| 113 | + if cfg.dbName != "" { |
| 114 | + basicConfig.Store.DBName = cfg.dbName |
| 115 | + } |
| 116 | + |
| 117 | + // Update the parse rules based on the extraction arguments |
| 118 | + for _, arg := range cfg.extractArgs { |
| 119 | + switch arg { |
| 120 | + case "NeedScope": |
| 121 | + basicConfig.ParseRule.NeedScope = true |
| 122 | + case "NoNeedTests": |
| 123 | + basicConfig.ParseRule.NeedTests = false |
| 124 | + case "NeedModFile": |
| 125 | + basicConfig.ParseRule.NeedModFile = true |
| 126 | + case "NeedCompile": |
| 127 | + basicConfig.ParseRule.NeedCompile = true |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return basicConfig |
| 132 | +} |
0 commit comments