Beispiel #1
0
func main() {
	kingpin.Parse()
	adjustedSelection := ShrinkToNonWhiteSpace(selectionFromString(*selection), util.ReadFileAsStringOrPanic(*inputFilename))
	if *outputFilename == "" {
		fmt.Println(ExtractFileToString(*inputFilename, adjustedSelection, *funcName, false))
	} else {
		ExtractFileToFile(*inputFilename, adjustedSelection, *funcName, *outputFilename, false)
	}
}
Beispiel #2
0
func extractionDataFrom(filename string) (Selection, string) {
	parts := strings.Split(strings.TrimRight(util.ReadFileAsStringOrPanic(filename), "\n"), " ")
	Expect(parts).To(HaveLen(5))
	return Selection{
			Position{toInt(parts[0]), toInt(parts[1])},
			Position{toInt(parts[2]), toInt(parts[3])},
		},
		parts[4]
}
Beispiel #3
0
func ExtractFileToFile(inputFileName string, selection Selection, extractedFuncName string, outputFilename string, debugOutput bool) {
	fileSet, astFile := astFromFile(inputFileName)
	if debugOutput {
		createAstFileDump(inputFileName+".ast", fileSet, astFile)
	}
	doExtraction(fileSet, astFile, selection, extractedFuncName)
	util.WriteFileAsStringOrPanic(outputFilename, stringFrom(fileSet, astFile))
	err := exec.Command("gofmt", "-w", outputFilename).Run()
	if err != nil {
		fmt.Fprintln(os.Stderr, util.ReadFileAsStringOrPanic(outputFilename))
		panic(err)
	}
}
Beispiel #4
0
func lineLengthsFrom(fileSet *token.FileSet) []int {
	return lineLengthsFromLines(strings.Split(util.ReadFileAsStringOrPanic(fileSet.File(1).Name()), "\n"))
}
Beispiel #5
0
package main_test

import (
	. "github.com/petergtz/goextract"
	"github.com/petergtz/goextract/util"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Selection", func() {
	It("Shrinks correctly", func() {
		Expect(ShrinkToNonWhiteSpace(
			Selection{Begin: Position{4, 1}, End: Position{6, 1}},
			util.ReadFileAsStringOrPanic("test_data/shrink_selection"))).
			To(Equal(Selection{Begin: Position{5, 2}, End: Position{5, 8}}))
	})
})
Beispiel #6
0
func expectedOutputFor(prefix string) string {
	return util.ReadFileAsStringOrPanic(filepath.Join("test_data", prefix) + ".output")
}