mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-05-24 16:21:14 -03:00
generator: skip symlinks on *nix and win
As Git on win does not support symlinks [1], we have to hard-code the paths to fils under ./samples/ in Linguist codebase that are known to be a symlink. 1. https://github.com/git-for-windows/git/wiki/Symbolic-Links TestPlan: - go test ./internal/code-generator/generator -run Test_GeneratorTestSuite Signed-off-by: Alexander Bezzubov <bzz@apache.org>
This commit is contained in:
parent
9c082eb2d4
commit
9be0211f04
@ -11,6 +11,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/go-enry/go-enry/v2/internal/tokenizer"
|
"github.com/go-enry/go-enry/v2/internal/tokenizer"
|
||||||
@ -107,49 +108,46 @@ func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readSamples collects ./samples/ filenames from the Linguist codebase, skiping symlinks.
|
||||||
func readSamples(samplesLangDir string) ([]string, error) {
|
func readSamples(samplesLangDir string) ([]string, error) {
|
||||||
const samplesLangFilesDir = "filenames"
|
const specialSubDir = "filenames"
|
||||||
sampleFiles, err := ioutil.ReadDir(samplesLangDir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var samples []string
|
var samples []string
|
||||||
for _, sampleFile := range sampleFiles {
|
|
||||||
filename := filepath.Join(samplesLangDir, sampleFile.Name())
|
|
||||||
if sampleFile.Mode().IsRegular() {
|
|
||||||
samples = append(samples, filename)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if sampleFile.IsDir() && sampleFile.Name() == samplesLangFilesDir {
|
err := filepath.Walk(samplesLangDir, func(path string, info os.FileInfo, err error) error {
|
||||||
subSamples, err := readSubSamples(filename)
|
if err != nil {
|
||||||
if err != nil {
|
fmt.Printf("failure accessing a path %q: %v\n", path, err)
|
||||||
return nil, err
|
return err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
switch info.Name() {
|
||||||
|
case filepath.Base(samplesLangDir):
|
||||||
|
return nil
|
||||||
|
case specialSubDir:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
|
|
||||||
samples = append(samples, subSamples...)
|
|
||||||
}
|
}
|
||||||
|
// skip git file symlinks on win and *nix
|
||||||
|
if isKnownSymlinkInLinguist(path) || !info.Mode().IsRegular() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
samples = append(samples, path)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
}
|
return samples, err
|
||||||
|
|
||||||
return samples, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSubSamples(path string) ([]string, error) {
|
// isKnownSymlinkInLinguist checks if the file name is on the list of known symlinks.
|
||||||
subSamples := []string{}
|
// On Windows, there is no symlink support in Git [1] and those become regular text files,
|
||||||
entries, err := ioutil.ReadDir(path)
|
// so we have to skip these files manually, maintaing a list here :/
|
||||||
if err != nil {
|
// 1. https://github.com/git-for-windows/git/wiki/Symbolic-Links
|
||||||
return nil, err
|
//
|
||||||
}
|
// $ find -L .linguist/samples -xtype l
|
||||||
|
func isKnownSymlinkInLinguist(path string) bool {
|
||||||
for _, entry := range entries {
|
return strings.HasSuffix(path, filepath.Join("Ant Build System", "filenames", "build.xml")) ||
|
||||||
if entry.Mode().IsRegular() {
|
strings.HasSuffix(path, filepath.Join("Markdown", "symlink.md"))
|
||||||
subSamples = append(subSamples, filepath.Join(path, entry.Name()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return subSamples, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTokens(samples []string) ([]string, error) {
|
func getTokens(samples []string) ([]string, error) {
|
Loading…
x
Reference in New Issue
Block a user