示例#1
0
文件: parser.go 项目: kego/ke
func scanForTypesAndExports(ctx context.Context, env *envctx.Env, cache *sysctx.SysPackageInfo, hash *PackageHasher) error {

	// While we're scanning for types, we should use a custom unpacking env,
	// because the env from the context is the one of the local package.

	files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	localContext := envctx.NewContext(ctx, env)
	for b := range bytes {
		if b.Err != nil {
			return kerr.Wrap("JACKALTIGG", b.Err)
		}

		o := &system.ObjectStub{}
		if err := system.Unmarshal(localContext, b.Bytes, o); err != nil {
			return kerr.Wrap("HCYGNBDFFA", err)
		}
		if o.Type == nil {
			return kerr.New("NUKWIHYFMQ", "%s has no type", b.File)
		}
		if o.Id == nil && *o.Type != *system.NewReference("kego.io/system", "package") {
			// we tolerate missing ID only for system:package
			return kerr.New("DLLMKTDYFW", "%s has no id", b.File)
		}
		relativeFile, err := filepath.Rel(env.Dir, b.File)
		if err != nil {
			return kerr.Wrap("AWYRJSCYQS", err)
		}
		switch *o.Type {
		case *system.NewReference("kego.io/system", "type"):
			if err := ProcessTypeFileBytes(ctx, env, relativeFile, b.Bytes, cache, hash); err != nil {
				return kerr.Wrap("IVEFDDSKHE", err)
			}
		case *system.NewReference("kego.io/system", "package"):
			cache.PackageBytes = b.Bytes
			cache.PackageFilename = relativeFile
		default:
			cache.Globals.Set(o.Id.Name, relativeFile)
			if o.Export {
				cache.Exports.Set(o.Id.Name, o.Type.Name, o.Type.Package, b.Bytes)
				if hash != nil {
					hash.Exports[o.Id.Name+" "+o.Type.Name+" "+o.Type.Package] = cityhash.CityHash64(b.Bytes, uint32(len(b.Bytes)))
				}
			}
		}

	}
	return nil
}
示例#2
0
func testUnpack(t *testing.T, path string) {
	ctx, _, err := process.Initialise(context.Background(), &process.Options{
		Path: path,
	})
	require.NoError(t, err)

	env := envctx.FromContext(ctx)

	files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	for b := range bytes {
		_, err := node.Unmarshal(ctx, b.Bytes)
		require.NoError(t, err, b.File)
	}
}
示例#3
0
文件: package.go 项目: kego/ke
func Scan(ctx context.Context, path string) (*Package, error) {
	p := new(Package)
	// use a new system context for the duration of the scan
	ctx = sysctx.NewContext(ctx)
	env, err := parser.ScanForEnv(ctx, path)
	if err != nil {
		return nil, kerr.Wrap("SFPSTLPKMX", err)
	}
	p.Env = env
	files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	localContext := envctx.NewContext(ctx, env)
	for b := range bytes {
		f := new(File)
		f.Package = p
		if b.Err != nil {
			return nil, kerr.Wrap("IPUHPBBWEA", b.Err)
		}
		o := &system.ObjectStub{}
		if err := system.Unmarshal(localContext, b.Bytes, o); err != nil {
			return nil, kerr.Wrap("DCAGIDLXRT", err)
		}
		if o.Type == nil {
			return nil, kerr.New("AJPCEQTWPS", "%s has no type", b.File)
		}
		if o.Id == nil && *o.Type != *system.NewReference("kego.io/system", "package") {
			// we tolerate missing ID only for system:package
			return nil, kerr.New("YDKYLXTGYC", "%s has no id", b.File)
		}
		f.AbsoluteFilepath = b.File
		f.Type = o.Type
		f.Id = o.Id
		if f.RelativeFilepath, err = filepath.Rel(env.Dir, b.File); err != nil {
			return nil, kerr.Wrap("QDAEGOWTWP", err)
		}
		f.Directory, f.Filename = filepath.Split(b.File)
		f.IsInRoot = DirEqual(f.Directory, env.Dir)
		f.Extension = filepath.Ext(b.File)
		f.Yaml = f.Extension == ".yaml" || f.Extension == ".yml"
		p.Files = append(p.Files, f)
	}
	return p, nil
}
示例#4
0
文件: validate.go 项目: kego/ke
func ValidatePackage(ctx context.Context) (errors []ValidationError, err error) {

	env := envctx.FromContext(ctx)

	files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	for c := range bytes {
		if c.Err != nil {
			return nil, kerr.Wrap("IHSVWAUAYW", c.Err)
		}
		ve, err := validateBytes(ctx, c.Bytes)
		if err != nil {
			return nil, kerr.Wrap("KWLWXKWHLF", err)
		}
		if len(ve) > 0 {
			errors = append(errors, ve...)
		}
	}

	return
}
示例#5
0
文件: parser.go 项目: kego/ke
func scanForPackage(ctx context.Context, env *envctx.Env) (*system.Package, error) {
	localContext := envctx.NewContext(ctx, env)
	files := scanner.ScanDirToFiles(ctx, env.Dir, false)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	for b := range bytes {
		if b.Err != nil {
			return nil, kerr.Wrap("GATNNQKNHY", b.Err, b.File)
		}
		o := &system.ObjectStub{}
		if err := system.Unmarshal(localContext, b.Bytes, o); err != nil {
			switch kerr.Source(err).(type) {
			case system.UnknownPackageError, system.UnknownTypeError:
				// don't return error
			default:
				return nil, kerr.Wrap("MTDCXBYBEJ", err, b.File)
			}
		}
		if o.Type == nil {
			return nil, kerr.New("MSNIGTIDIO", "%s has no type", b.File)
		}
		switch *o.Type {
		case *system.NewReference("kego.io/system", "package"):
			var i interface{}
			err := system.Unmarshal(localContext, b.Bytes, &i)
			if err != nil {
				switch kerr.Source(err).(type) {
				case system.UnknownPackageError, system.UnknownTypeError:
					// don't return error
				default:
					return nil, kerr.Wrap("XTEQCAYQJP", err)
				}
			}
			if pkg, ok := i.(*system.Package); ok {
				return pkg, nil
			}
		}
	}
	return nil, nil
}