github.com/zkry/enry@v1.6.3/internal/code-generator/main.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  
     7  	"gopkg.in/src-d/enry.v1/internal/code-generator/generator"
     8  )
     9  
    10  const (
    11  	// languages info file
    12  	languagesYAML = ".linguist/lib/linguist/languages.yml"
    13  
    14  	// linguist's samples directory
    15  	samplesDir = ".linguist/samples"
    16  
    17  	// extension.go generation
    18  	extensionsFile     = "data/extension.go"
    19  	extensionsTmplPath = "internal/code-generator/assets/extension.go.tmpl"
    20  	extensionsTmpl     = "extension.go.tmpl"
    21  
    22  	// content.go generation
    23  	heuristicsRuby  = ".linguist/lib/linguist/heuristics.rb"
    24  	contentFile     = "data/content.go"
    25  	contentTmplPath = "internal/code-generator/assets/content.go.tmpl"
    26  	contentTmpl     = "content.go.tmpl"
    27  
    28  	// vendor.go generation
    29  	vendorYAML     = ".linguist/lib/linguist/vendor.yml"
    30  	vendorFile     = "data/vendor.go"
    31  	vendorTmplPath = "internal/code-generator/assets/vendor.go.tmpl"
    32  	vendorTmpl     = "vendor.go.tmpl"
    33  
    34  	// documentation.go generation
    35  	documentationYAML     = ".linguist/lib/linguist/documentation.yml"
    36  	documentationFile     = "data/documentation.go"
    37  	documentationTmplPath = "internal/code-generator/assets/documentation.go.tmpl"
    38  	documentationTmpl     = "documentation.go.tmpl"
    39  
    40  	// type.go generation
    41  	typeFile     = "data/type.go"
    42  	typeTmplPath = "internal/code-generator/assets/type.go.tmpl"
    43  	typeTmpl     = "type.go.tmpl"
    44  
    45  	// interpreter.go generation
    46  	interpretersFile     = "data/interpreter.go"
    47  	interpretersTmplPath = "internal/code-generator/assets/interpreter.go.tmpl"
    48  	interpretersTmpl     = "interpreter.go.tmpl"
    49  
    50  	// filename.go generation
    51  	filenamesFile     = "data/filename.go"
    52  	filenamesTmplPath = "internal/code-generator/assets/filename.go.tmpl"
    53  	filenamesTmpl     = "filename.go.tmpl"
    54  
    55  	// alias.go generation
    56  	aliasesFile     = "data/alias.go"
    57  	aliasesTmplPath = "internal/code-generator/assets/alias.go.tmpl"
    58  	aliasesTmpl     = "alias.go.tmpl"
    59  
    60  	// frequencies.go generation
    61  	frequenciesFile     = "data/frequencies.go"
    62  	frequenciesTmplPath = "internal/code-generator/assets/frequencies.go.tmpl"
    63  	frequenciesTmpl     = "frequencies.go.tmpl"
    64  
    65  	// commit.go generation
    66  	commitFile     = "data/commit.go"
    67  	commitTmplPath = "internal/code-generator/assets/commit.go.tmpl"
    68  	commitTmpl     = "commit.go.tmpl"
    69  
    70  	// mimeType.go generation
    71  	mimeTypeFile     = "data/mimeType.go"
    72  	mimeTypeTmplPath = "internal/code-generator/assets/mimeType.go.tmpl"
    73  	mimeTypeTmpl     = "mimeType.go.tmpl"
    74  
    75  	commitPath = ".linguist/.git/HEAD"
    76  )
    77  
    78  type generatorFiles struct {
    79  	generate    generator.File
    80  	fileToParse string
    81  	samplesDir  string
    82  	outPath     string
    83  	tmplPath    string
    84  	tmplName    string
    85  	commit      string
    86  }
    87  
    88  func main() {
    89  	commit, err := getCommit(commitPath)
    90  	if err != nil {
    91  		log.Printf("couldn't find commit: %v", err)
    92  	}
    93  
    94  	fileList := []*generatorFiles{
    95  		&generatorFiles{generator.Extensions, languagesYAML, "", extensionsFile, extensionsTmplPath, extensionsTmpl, commit},
    96  		&generatorFiles{generator.Heuristics, heuristicsRuby, "", contentFile, contentTmplPath, contentTmpl, commit},
    97  		&generatorFiles{generator.Vendor, vendorYAML, "", vendorFile, vendorTmplPath, vendorTmpl, commit},
    98  		&generatorFiles{generator.Documentation, documentationYAML, "", documentationFile, documentationTmplPath, documentationTmpl, commit},
    99  		&generatorFiles{generator.Types, languagesYAML, "", typeFile, typeTmplPath, typeTmpl, commit},
   100  		&generatorFiles{generator.Interpreters, languagesYAML, "", interpretersFile, interpretersTmplPath, interpretersTmpl, commit},
   101  		&generatorFiles{generator.Filenames, languagesYAML, samplesDir, filenamesFile, filenamesTmplPath, filenamesTmpl, commit},
   102  		&generatorFiles{generator.Aliases, languagesYAML, "", aliasesFile, aliasesTmplPath, aliasesTmpl, commit},
   103  		&generatorFiles{generator.Frequencies, "", samplesDir, frequenciesFile, frequenciesTmplPath, frequenciesTmpl, commit},
   104  		&generatorFiles{generator.Commit, "", "", commitFile, commitTmplPath, commitTmpl, commit},
   105  		&generatorFiles{generator.MimeType, languagesYAML, "", mimeTypeFile, mimeTypeTmplPath, mimeTypeTmpl, commit},
   106  	}
   107  
   108  	for _, file := range fileList {
   109  		if err := file.generate(file.fileToParse, file.samplesDir, file.outPath, file.tmplPath, file.tmplName, file.commit); err != nil {
   110  			log.Println(err)
   111  		}
   112  	}
   113  }
   114  
   115  func getCommit(path string) (string, error) {
   116  	commit, err := ioutil.ReadFile(path)
   117  	if err != nil {
   118  		return "", err
   119  	}
   120  
   121  	if string(commit) == "ref: refs/heads/master\n" {
   122  		path = ".linguist/.git/" + string(commit[5:len(commit)-1])
   123  		commit, err = ioutil.ReadFile(path)
   124  		if err != nil {
   125  			return "", err
   126  		}
   127  	}
   128  
   129  	return string(commit[:len(commit)-1]), nil
   130  }