Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
gop / usr / lib / gop / cmd / chore / gopminispec / minispec.gop
Size: Mime:
/*
 * Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import (
	"flag"
	"gop/doc/spec/mini"
	"gop/tpl"
	"gop/tpl/matcher"
	"gop/x/gopprojs"
	"os"
	"path/filepath"
)

func isGopFile(file string) bool {
	if len(file) > 4 {
		switch file[len(file)-4:] {
		case ".gop", ".gox", ".gsh", ".spx", ".yap":
			return true
		}
	}
	return false
}

func parseFiles(files []string, dumpAST bool) (nErr int) {
	for file in files {
		fprintln os.Stderr, "\n==> Parsing ${file}"
		f, err := mini.parseFile(nil, file, nil)
		if err != nil {
			fprintln os.Stderr, err
			nErr++
		} else if dumpAST {
			tpl.dump f
		}
	}
	return
}

func parseDir(dir string, recursively, dumpAST bool) (nErr int) {
	filepath.walkDir dir, (file, d, err) => {
		if err != nil {
			return err
		} else if fname := d.name; d.isDir {
			if recursively {
				if fname.hasPrefix("_") || fname == "fullspec" {
					return filepath.SkipDir
				}
			} else if file != dir {
				return filepath.SkipDir
			}
		} else if !fname.hasPrefix("_") && isGopFile(fname) {
			fprintln os.Stderr, "\n==> Parsing ${file}"
			f, err := mini.parseFile(nil, file, nil)
			if err != nil {
				fprintln os.Stderr, err
				nErr++
			} else if dumpAST {
				tpl.dump f
			}
		}
		return nil
	}
	return
}

var (
	flagVerbose = flag.Bool("v", false, "print verbose information")
	flagDumpAST = flag.Bool("ast", false, "dump AST")
)

flag.parse

pattern := flag.args
if pattern.len == 0 {
	echo "Usage: gopminispec [-v -ast] packages"
	return
}

if *flagVerbose {
	matcher.setDebug matcher.DbgFlagAll
}

dumpAST := *flagDumpAST
nErr := 0

projs, err := gopprojs.parseAll(pattern...)
if err != nil {
	fprintln os.Stderr, err
	os.exit 1
}

for proj in projs {
	switch v := proj.(type) {
	case *gopprojs.DirProj:
		dir := v.Dir
		recursively := dir.hasSuffix("/...")
		if recursively {
			dir = dir[:len(dir)-4]
		}
		nErr += parseDir(dir, recursively, dumpAST)
	case *gopprojs.FilesProj:
		nErr += parseFiles(v.Files, dumpAST)
	default:
		panic "only support local files and directories"
	}
}
fprintln os.Stderr

if nErr > 0 {
	os.exit 1
}