forked from imjerrybao/apex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
87 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,100 @@ | ||
package clojure | ||
package clojure | ||
|
||
import ( | ||
azip "archive/zip" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/apex/apex/archive" | ||
"github.com/apex/apex/function" | ||
azip "archive/zip" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/apex/apex/archive" | ||
"github.com/apex/apex/function" | ||
) | ||
|
||
func init() { | ||
function.RegisterPlugin("clojure", &Plugin{}) | ||
} | ||
|
||
const ( | ||
// Runtime name used by Apex | ||
Runtime = "clojure" | ||
|
||
// RuntimeCanonical represents names used by AWS Lambda | ||
RuntimeCanonical = "java8" | ||
|
||
jarFile = "apex.jar" | ||
) | ||
|
||
func init() { | ||
function.RegisterPlugin("clojure", &Plugin{}) | ||
} | ||
|
||
const ( | ||
// Runtime name used by Apex | ||
Runtime = "clojure" | ||
// RuntimeCanonical represents names used by AWS Lambda | ||
RuntimeCanonical = "java8" | ||
jarFile = "apex.jar" | ||
) | ||
|
||
// Plugin Does plugin things | ||
type Plugin struct{} | ||
// Open adds the shim and golang defaults. | ||
func (p *Plugin) Open(fn *function.Function) error { | ||
if fn.Runtime != Runtime { | ||
return nil | ||
} | ||
if fn.Hooks.Build == "" { | ||
fn.Hooks.Build = "lein uberjar && mv target/*-standalone.jar target/apex.jar" | ||
} | ||
if fn.Hooks.Clean == "" { | ||
fn.Hooks.Clean = "rm -f target &> /dev/null" | ||
} | ||
|
||
if _, err := os.Stat(".apexignore"); err != nil { | ||
// Since we're deploying a fat jar, we don't need anything else. | ||
fn.IgnoreFile = []byte(` | ||
type Plugin struct{} | ||
|
||
// Open adds the shim and golang defaults. | ||
func (p *Plugin) Open(fn *function.Function) error { | ||
if fn.Runtime != Runtime { | ||
return nil | ||
} | ||
|
||
if fn.Hooks.Build == "" { | ||
fn.Hooks.Build = "lein uberjar && mv target/*-standalone.jar target/apex.jar" | ||
} | ||
|
||
if fn.Hooks.Clean == "" { | ||
fn.Hooks.Clean = "rm -f target &> /dev/null" | ||
} | ||
|
||
if _, err := os.Stat(".apexignore"); err != nil { | ||
// Since we're deploying a fat jar, we don't need anything else. | ||
fn.IgnoreFile = []byte(` | ||
* | ||
!**/apex.jar | ||
`) | ||
} | ||
} | ||
|
||
return nil | ||
return nil | ||
} | ||
|
||
// Build adds the jar contents to zipfile. | ||
func (p *Plugin) Build(fn *function.Function, zip *archive.Zip) error { | ||
if fn.Runtime != Runtime { | ||
return nil | ||
} | ||
fn.Runtime = RuntimeCanonical | ||
|
||
expectedJarPath := filepath.Join(fn.Path, "target", jarFile) | ||
if _, err := os.Stat(expectedJarPath); err != nil { | ||
return errors.New("Expected jar file not found") | ||
} | ||
fn.Log.Debugf("found jar path: %s", expectedJarPath) | ||
|
||
fn.Log.Debug("appending compiled files") | ||
reader, err := azip.OpenReader(expectedJarPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer reader.Close() | ||
|
||
for _, file := range reader.File { | ||
parts := strings.Split(file.Name, ".") | ||
extension := parts[len(parts) - 1] | ||
if extension == "clj" || extension == "cljx" || extension == "cljc" { | ||
continue | ||
} | ||
|
||
r, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return err | ||
} | ||
r.Close() | ||
|
||
zip.AddBytes(file.Name, b) | ||
} | ||
|
||
return nil | ||
if fn.Runtime != Runtime { | ||
return nil | ||
} | ||
fn.Runtime = RuntimeCanonical | ||
|
||
jar := filepath.Join(fn.Path, "target", jarFile) | ||
if _, err := os.Stat(jar); err != nil { | ||
return errors.Errorf("missing jar file %q", jar) | ||
} | ||
|
||
fn.Log.Debug("appending compiled files") | ||
reader, err := azip.OpenReader(jar) | ||
if err != nil { | ||
return errors.Wrap(err, "opening zip") | ||
} | ||
defer reader.Close() | ||
|
||
for _, file := range reader.File { | ||
parts := strings.Split(file.Name, ".") | ||
ext := parts[len(parts)-1] | ||
|
||
if ext == "clj" || ext == "cljx" || ext == "cljc" { | ||
continue | ||
} | ||
|
||
r, err := file.Open() | ||
if err != nil { | ||
return errors.Wrap(err, "opening file") | ||
} | ||
|
||
b, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return errors.Wrap(err, "reading file") | ||
} | ||
r.Close() | ||
|
||
zip.AddBytes(file.Name, b) | ||
} | ||
|
||
return nil | ||
} |