Add Abs method on Document
This commit is contained in:
parent
17b6253211
commit
3c9f91a2c9
33
document.go
33
document.go
@ -1,5 +1,6 @@
|
|||||||
package step
|
package step
|
||||||
|
|
||||||
|
import "os"
|
||||||
import "io"
|
import "io"
|
||||||
import "time"
|
import "time"
|
||||||
import "strings"
|
import "strings"
|
||||||
@ -57,22 +58,44 @@ func (this *Document) Environment () *Environment {
|
|||||||
|
|
||||||
// Abs returns an absolute representation of the given path. If the path is an
|
// Abs returns an absolute representation of the given path. If the path is an
|
||||||
// absolute path already, it is returned as-is. If the path is a relative path,
|
// absolute path already, it is returned as-is. If the path is a relative path,
|
||||||
// it is treated as relative to the current working directory. If the path is an
|
// it is treated as relative to the current working directory. If the path is a
|
||||||
// absolute path beginning with "." or "..", it is treated as relative to this
|
// relative path beginning with "." or "..", it is treated as relative to this
|
||||||
// document. The result is cleaned.
|
// document. The result is cleaned.
|
||||||
func (this *Document) Abs (name string) (string, error) {
|
func (this *Document) Abs (name string) (string, error) {
|
||||||
if filepath.IsAbs(name) {
|
if filepath.IsAbs(name) {
|
||||||
return filepath.Clean(name), nil
|
return filepath.Clean(name), nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(name, ".") {
|
if strings.HasPrefix(name, ".") {
|
||||||
|
directory := this.dir()
|
||||||
|
return filepath.Abs(filepath.Join(directory, name))
|
||||||
|
}
|
||||||
|
return filepath.Abs(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rel returns the given path relative to the current working directory. As a
|
||||||
|
// special case, any relative path beginning with "." or ".." is treated as
|
||||||
|
// being relative to this document before it is made relative to the current
|
||||||
|
// working directory. The result is cleaned.
|
||||||
|
func (this *Document) Rel (name string) (string, error) {
|
||||||
|
if filepath.IsAbs(name) {
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil { return "", err }
|
||||||
|
return filepath.Rel(cwd, name)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(name, ".") {
|
||||||
|
directory := this.dir()
|
||||||
|
return filepath.Join(directory, name), nil
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Document) dir () string {
|
||||||
directory := this.name
|
directory := this.name
|
||||||
ext := filepath.Ext(directory)
|
ext := filepath.Ext(directory)
|
||||||
if ext != "" {
|
if ext != "" {
|
||||||
directory = filepath.Dir(directory)
|
directory = filepath.Dir(directory)
|
||||||
}
|
}
|
||||||
return filepath.Abs(filepath.Join(directory, name))
|
return directory
|
||||||
}
|
|
||||||
return filepath.Abs(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecutionData is data made available to documents as they are being exeucted.
|
// ExecutionData is data made available to documents as they are being exeucted.
|
||||||
|
@ -31,3 +31,29 @@ func TestDocumentAbs (test *testing.T) {
|
|||||||
test.Log(path5)
|
test.Log(path5)
|
||||||
if path5 != "/something/thing" { test.Fatal("incorrect path") }
|
if path5 != "/something/thing" { test.Fatal("incorrect path") }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDocumentRel (test *testing.T) {
|
||||||
|
document := Document {
|
||||||
|
name: "foo/bar.step",
|
||||||
|
}
|
||||||
|
path1, err := document.Rel("thing.step")
|
||||||
|
if err != nil { test.Fatal(err) }
|
||||||
|
path2, err := document.Rel("thing/other.step")
|
||||||
|
if err != nil { test.Fatal(err) }
|
||||||
|
path3, err := document.Rel("./thing/other.step")
|
||||||
|
if err != nil { test.Fatal(err) }
|
||||||
|
path4, err := document.Rel("../thing/other.step")
|
||||||
|
if err != nil { test.Fatal(err) }
|
||||||
|
path5, err := document.Rel("/something/thing")
|
||||||
|
if err != nil { test.Fatal(err) }
|
||||||
|
test.Log(path1)
|
||||||
|
if path1 != filepath.Join("thing.step") { test.Fatal("incorrect path") }
|
||||||
|
test.Log(path2)
|
||||||
|
if path2 != filepath.Join("thing/other.step") { test.Fatal("incorrect path") }
|
||||||
|
test.Log(path3)
|
||||||
|
if path3 != filepath.Join("foo/thing/other.step") { test.Fatal("incorrect path") }
|
||||||
|
test.Log(path4)
|
||||||
|
if path4 != filepath.Join("thing/other.step") { test.Fatal("incorrect path") }
|
||||||
|
test.Log(path5)
|
||||||
|
if path5 == "/something/thing" { test.Fatal("incorrect path") }
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user