From fe871d48d639b544ae6b318b94601649342eb5e9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 5 Feb 2018 15:50:56 -0500 Subject: [PATCH] improved robustness more tests added. --- ps.go | 54 +++++++++++++++++++++++++++++++++++++++--------- ps_test.go | 45 +++++++++++++++++----------------------- scripts/dojs.vbs | 12 +++++------ scripts/test.jsx | 9 ++++---- scripts/test.vbs | 4 +++- 5 files changed, 76 insertions(+), 48 deletions(-) diff --git a/ps.go b/ps.go index 0136426..ef271f1 100644 --- a/ps.go +++ b/ps.go @@ -1,5 +1,3 @@ -// +build windows - // Package ps lets you manipulate Adobe Photoshop (CS5) from go. // This is primarily done by calling VBS/Applescript files. // @@ -8,9 +6,12 @@ package ps import ( "bytes" + "errors" "fmt" + "io/ioutil" + "os" "os/exec" - "path" + "path/filepath" "runtime" "strings" ) @@ -21,7 +22,7 @@ var pkgpath string func init() { _, file, _, _ := runtime.Caller(0) - pkgpath = path.Dir(file) + pkgpath = filepath.Dir(file) switch runtime.GOOS { case "windows": Cmd = "cscript.exe" @@ -53,8 +54,29 @@ func Quit(save int) ([]byte, error) { return run("quit", string(save)) } -func Js(args ...string) ([]byte, error) { - return run("dojs", args...) +func Js(path string, args ...string) ([]byte, error) { + // Temp file for js to output to. + outpath := filepath.Join(os.Getenv("TEMP"), "js_out.txt") + args = append([]string{outpath}, args...) + + // If passed a script by name, assume it's in the default folder. + if filepath.Dir(path) == "." { + path = filepath.Join(pkgpath, "scripts", path) + } + + args = append([]string{path}, args...) + cmd, err := run("dojs", args...) + if err != nil { + return []byte{}, err + } + file, err := ioutil.ReadFile(outpath) + if err != nil { + // fmt.Println(cmd) + return cmd, err + } + cmd = append(cmd, file...) + // os.Remove(outpath) + return cmd, err } func Wait(msg string) { @@ -66,7 +88,6 @@ func Wait(msg string) { func run(name string, args ...string) ([]byte, error) { var ext string var out bytes.Buffer - var stderr bytes.Buffer switch runtime.GOOS { case "windows": @@ -77,10 +98,23 @@ func run(name string, args ...string) ([]byte, error) { if !strings.HasSuffix(name, ext) { name += ext } - args = append([]string{Opts, path.Join(pkgpath, "scripts", name)}, args...) + + if strings.Contains(name, "dojs") { + args = append([]string{Opts, filepath.Join(pkgpath, "scripts", name)}, + args[0], + fmt.Sprintf("%s", strings.Join(args[1:], ",")), + ) + } else { + args = append([]string{Opts, filepath.Join(pkgpath, "scripts", name)}, args...) + } + cmd := exec.Command(Cmd, args...) cmd.Stdout = &out - cmd.Stderr = &stderr + cmd.Stderr = &out err := cmd.Run() - return out.Bytes(), err + if err != nil { + return []byte{}, errors.New(string(out.Bytes())) + } else { + return out.Bytes(), err + } } diff --git a/ps_test.go b/ps_test.go index d412dd5..e6ca08b 100644 --- a/ps_test.go +++ b/ps_test.go @@ -2,7 +2,6 @@ package ps import ( "fmt" - _ "io/ioutil" "os" "path/filepath" _ "strings" @@ -20,24 +19,24 @@ func TestOpen(t *testing.T) { if testing.Short() { t.Skip("Skipping \"TestOpen\"") } - Open("F:\\GitLab\\dreamkeepers-psd\\Template009.1.psd") -} - -// TODO: Comparison borked -/*func TestRun(t *testing.T) { - out := []byte("Testing...\n") - msg, err := run("test") + _, err := Open("F:\\GitLab\\dreamkeepers-psd\\Template009.1.psd") if err != nil { t.Fatal(err) } - if string(msg) == string(out) { - fail := fmt.Sprintf("run(test)\ngot:\t\"%s\"\nwant:\t\"%s\"\n", msg, out) +} + +func TestRun(t *testing.T) { + out := []byte("hello,\r\nworld!\r\n") + msg, err := run("test", "hello,", "world!") + if err != nil { + t.Fatal(err) + } + if string(msg) != string(out) { + fail := fmt.Sprintf("TestRun faild.\ngot:\n\"%s\"\nwant:\n\"%s\"\n", msg, out) t.Fatal(fail) - } else { - os.Remove(path.Join(pkgpath, "scripts", "test.txt")) } } -*/ + func TestQuit(t *testing.T) { if testing.Short() { t.Skip("Skipping \"TestQuit\"") @@ -50,21 +49,15 @@ func TestWait(t *testing.T) { fmt.Println() } -// TODO: Comparison borked -/*func TestJS(t *testing.T) { - out := "Testing...\n" - _, err := Js(path.Join(Folder, "test.jsx"), Folder) +func TestJS(t *testing.T) { + out := []byte("F:\\TEMP\\js_out.txt\r\narg\r\nargs\r\n") + script := "test.jsx" + ret, err := Js(script, "arg", "args") if err != nil { t.Fatal(err) } - f, err := ioutil.ReadFile(path.Join(Folder, "test.txt")) - if err != nil { - t.Fatal(err) - } - if strings.Compare(string(f), string(out)) != 0 { - fmt.Println(f) - fmt.Println([]byte(out)) - fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", f, out) + if string(ret) != string(out) { + fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", ret, out) t.Fatal(fail) } -}*/ +} diff --git a/scripts/dojs.vbs b/scripts/dojs.vbs index 222155b..6819bfd 100644 --- a/scripts/dojs.vbs +++ b/scripts/dojs.vbs @@ -1,10 +1,10 @@ -Dim app -Set app = CreateObject("Photoshop.Application") -if WScript.Arguments.Count = 0 then - WScript.Echo "Missing parameters" +Dim appRef +Set appRef = CreateObject("Photoshop.Application") +if wScript.Arguments.Count = 0 then + wScript.Echo "Missing parameters" else path = wScript.Arguments(0) - folder = wScript.Arguments(1) - app.DoJavaScriptFile path, Array(Folder) + args = wScript.Arguments(1) + appRef.DoJavaScriptFile path, Split(args, ",") end if \ No newline at end of file diff --git a/scripts/test.jsx b/scripts/test.jsx index 98d79ac..bc0c818 100644 --- a/scripts/test.jsx +++ b/scripts/test.jsx @@ -1,11 +1,10 @@ -var Path = arguments[0]; -alert(Path) -var saveFile = File(Path + "/test.txt"); - +var saveFile = File(arguments[0]); if(saveFile.exists) saveFile.remove(); saveFile.encoding = "UTF8"; saveFile.open("e", "TEXT", "????"); -saveFile.writeln("Testing..."); +for (var i = 0; i < arguments.length; i++) { + saveFile.writeln(arguments[i]) +} saveFile.close(); \ No newline at end of file diff --git a/scripts/test.vbs b/scripts/test.vbs index 12d7bf1..b413227 100644 --- a/scripts/test.vbs +++ b/scripts/test.vbs @@ -1 +1,3 @@ -wScript.echo "Testing..." \ No newline at end of file +for i=0 to wScript.Arguments.length-1 + wScript.echo wScript.Arguments(i) +next \ No newline at end of file