imports

package module
v0.0.0-...-bf22b73 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2019 License: Apache-2.0 Imports: 10 Imported by: 3

README

Import extraction

CLI for fast import extraction.

Installation

To build the project from source, run:

git clone https://github.com/src-d/imports.git
cd imports
go install ./cmd/sourced-imports

Usage example:

sourced-imports --root ./project-dir/

Example output:

{"file":"LICENSE","lang":"Text"}
{"file":"README.md","lang":"Markdown"}
{"file":"cmd/sourced-imports/main.go","lang":"Go","imports":["flag","fmt","github.com/src-d/imports","github.com/src-d/imports/languages/all","os"]}
...

Using as library

It is possible to use imports as a library, but you'll need to add this workaround to your go.mod:

replace github.com/smacker/go-tree-sitter => github.com/dennwc/go-tree-sitter dev

This workaround is temporary and won't be required in the future.

Documentation

Index

Constants

View Source
const Separator = "/"

Variables

This section is empty.

Functions

func RegisterLanguage

func RegisterLanguage(lang Language)

Types

type Config

type Config struct {
	// Out is a destination to write JSON output to during Extract.
	Out io.Writer
	// Num is the maximal number of goroutines when extracting imports.
	// Zero value means use NumCPU.
	Num int
	// MaxSize is the maximal size of files in bytes that will be parsed.
	// For files larger than this, only a sample of this size will be used for language detection.
	// Library may use the sample to try extracting imports, or may return an empty list of imports.
	MaxSize int64
	// SymLinks is option that allows traversal over sym-links, be aware of potentials loops
	// if false - all symlinks will be skipped
	SymLinks bool
}

type Extractor

type Extractor struct {
	// contains filtered or unexported fields
}

func NewExtractor

func NewExtractor(c Config) *Extractor

NewExtractor creates an extractor with a given configuration. See Config for more details.

func (*Extractor) Extract

func (e *Extractor) Extract(root, rel string) error

Extract imports recursively from a given directory. The root is a root of the project's repository and rel is the relative path inside it that will be processed. Two paths exists to allow the library to potentially parse dependency manifest files that are usually located in the root of the project.

func (*Extractor) ExtractFrom

func (e *Extractor) ExtractFrom(path string, content []byte) (*File, error)

ExtractFrom extracts imports from a given file content, assuming it had a given path.

The path is used for language detection only. It won't access any files locally and won't fetch dependency manifests as Extract may do.

type File

type File struct {
	Path    string   `json:"file"`
	Lang    string   `json:"lang,omitempty"`
	Imports []string `json:"imports,omitempty"`
}

func Extract

func Extract(path string, content []byte) (*File, error)

Extract imports from a given file content, assuming it had a given path.

The path is used for language detection only. It won't access any files locally and won't fetch dependency manifests as Extract may do.

The function does no sampling for the file, it means if the file size is large, it may take a lot of time to parse it or even detect the language. Instead, passing a part of the file may be an option.

type Language

type Language interface {
	Aliases() []string
	Imports(content []byte) ([]string, error)
}

func LanguageByName

func LanguageByName(name string) Language

Directories

Path Synopsis
cmd
sourced-imports command
languages
all

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL