Esempio n. 1
0
func convertRef(current unit.SourceUnit, jref *Ref) (*graph.Ref, error) {
	repoURI, u, path, err := jref.Target.Resolve()
	if err == ErrSkipResolve {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}

	start, end, err := parseSpan(jref.Span)
	if err != nil {
		return nil, err
	}

	ref := &graph.Ref{
		SymbolRepo:     repoURI,
		SymbolUnitType: unit.Type(current),
		SymbolUnit:     u,
		SymbolPath:     path,
		Def:            jref.Def,
		File:           filepath.Join(jref.File),
		Start:          start,
		End:            end,
	}

	return ref, nil
}
Esempio n. 2
0
func (v *npmVersion) BuildResolver(dep *dep2.RawDependency, c *config.Repository) (*container.Command, error) {
	var npmDep npmDependency
	j, _ := json.Marshal(dep.Target)
	json.Unmarshal(j, &npmDep)

	dockerfile, err := v.baseDockerfile()
	if err != nil {
		return nil, err
	}
	dockerfile = append(dockerfile, []byte("\n\nRUN npm install --quiet -g deptool@~0.0.2\n")...)

	cmd := container.Command{
		Container: container.Container{
			Dockerfile: dockerfile,
			Cmd:        []string{"nodejs", "/usr/local/bin/npm-deptool", npmDep.Name + "@" + npmDep.Spec},
		},
		Transform: func(orig []byte) ([]byte, error) {
			// resolvedDep is output from npm-deptool.
			type npmDeptoolOutput struct {
				Name        string
				ResolvedURL string `json:"_resolved"`
				ID          string `json:"_id"`
				Repository  struct {
					Type string
					URL  string
				}
			}
			var resolvedDeps map[string]npmDeptoolOutput
			err := json.Unmarshal(orig, &resolvedDeps)
			if err != nil {
				return nil, err
			}

			if len(resolvedDeps) == 0 {
				return nil, fmt.Errorf("npm-deptool did not output anything for raw dependency %+v", dep)
			}

			var resolvedDep *npmDeptoolOutput
			for name, v := range resolvedDeps {
				if name == npmDep.Name {
					resolvedDep = &v
					break
				}
			}

			if resolvedDep == nil {
				return nil, fmt.Errorf("npm-deptool did not return info about npm package %q for raw dependency %+v: all %d resolved deps are %+v", npmDep.Name, dep, len(resolvedDeps), resolvedDeps)
			}

			var toRepoCloneURL, toRevSpec string
			if strings.HasPrefix(resolvedDep.ResolvedURL, "https://registry.npmjs.org/") {
				// known npm package, so the repository refers to it
				toRepoCloneURL = resolvedDep.Repository.URL
			} else {
				// external tarball, git repo url, etc., so the repository might
				// refer to the source repo (if this is a fork) or not be
				// present at all
				u, err := url.Parse(resolvedDep.ResolvedURL)
				if err != nil {
					return nil, err
				}
				toRevSpec = u.Fragment

				u.Fragment = ""
				toRepoCloneURL = u.String()
			}

			return json.Marshal(&dep2.ResolvedTarget{
				ToRepoCloneURL:  toRepoCloneURL,
				ToUnitType:      unit.Type((&CommonJSPackage{})),
				ToUnit:          resolvedDep.Name,
				ToVersionString: resolvedDep.ID,
				ToRevSpec:       toRevSpec,
			})
		},
	}
	return &cmd, nil
}