Skip to content

Commit

Permalink
issue #48: decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Gerasimov committed Nov 12, 2013
1 parent cb129c1 commit 7e46c96
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
7 changes: 6 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ const (
)

var (
slash = string (os.PathSeparator)

GOPATH = os.Getenv ("GOPATH")
GOPATH_SRC = GOPATH + slash + "src" + slash

templatesRoot = "src/github.com/cliohq/clio/templates"
applicationTemplatesPath = helpers.FixPath (templatesRoot + "/application")
generatorsTemplatesPath = helpers.FixPath (helpers.FixPath(os.Getenv("GOPATH") + "/" + templatesRoot + "/generators"))
generatorsTemplatesPath = helpers.FixPath (helpers.FixPath(GOPATH + "/" + templatesRoot + "/generators"))
)

const (
Expand Down
84 changes: 46 additions & 38 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ type Application struct {
name string
}

var (
GOPATH = os.Getenv ("GOPATH")
slash = string (os.PathSeparator)
GOPATH_SRC = GOPATH + slash + "src" + slash
)


/**
* High level abstraction for creating new app
Expand All @@ -34,13 +28,9 @@ func Create (appName string) {
app.createContainer ()

err := app.copyFileTree (
// source
strings.Join (
[]string { GOPATH, applicationTemplatesPath },
slash),

// destination
GOPATH_SRC + app.name)
GOPATH + slash + applicationTemplatesPath,
GOPATH_SRC + app.name,
)

if err != nil {
log.Fatal (err)
Expand Down Expand Up @@ -74,14 +64,53 @@ func checkContainer (appName string) {
*/
func (app *Application) createContainer () {
appPath := GOPATH_SRC + app.name
err := os.Mkdir(appPath, 0776); if err == nil {
if err := os.Mkdir(appPath, 0776); err == nil {
fmt.Println(green, " create:", reset, appPath)
} else {
log.Fatal (err)
}
}


/**
* Copying certain directory from applciation's templates
* to a new application's skeleton
*/
func (app *Application) copyDir (file os.FileInfo, destination, fromFilePath, toFilePath string) error {
if err := os.Mkdir(toFilePath, file.Mode ()); err == nil {
fmt.Println(green, " create:", reset, toFilePath)
} else {
return err
}

// scanning next level
newTo := strings.Join ([]string { destination, file.Name () }, slash)
if err := app.copyFileTree (fromFilePath, newTo); err != nil {
return err
}

return nil
}


/**
* Copying certain file from applciation's templates
* to a new application's skeleton
*/
func (app *Application) copyFile (file os.FileInfo, source, destination string) error {
fileData, err := ioutil.ReadFile (source); if err != nil {
return err
}

if err = ioutil.WriteFile (destination, []byte(fileData), file.Mode ()); err == nil {
fmt.Println(green, " create:", reset, destination)
} else {
return err
}
return nil
}


/**
* Creating an application sceleton from templates/application
*/
Expand All @@ -95,33 +124,12 @@ func (app *Application) copyFileTree (from, to string) error {
for _, f := range files {

fromFilePath := strings.Join ([]string { from, f.Name () }, slash)
toFilePath := strings.Join ([]string { to, f.Name () }, slash)
toFilePath := strings.Join ([]string { to, f.Name () }, slash)

if f.IsDir () == true {

// copying folders
err := os.Mkdir(toFilePath, f.Mode ()); if err != nil {
return err
} else {
fmt.Println(green, " create:", reset, toFilePath)
}

// scanning next level
newTo := strings.Join ([]string { to, f.Name () }, slash)
err = app.copyFileTree (fromFilePath, newTo); if err != nil {
return err
}
app.copyDir (f, to, fromFilePath, toFilePath)
} else {

// copying files
fileData, err := ioutil.ReadFile (fromFilePath); if err != nil {
return err
}
err = ioutil.WriteFile (toFilePath, []byte(fileData), f.Mode ()); if err != nil {
return err
} else {
fmt.Println(green, " create:", reset, toFilePath)
}
app.copyFile (f, fromFilePath, toFilePath)
}
}

Expand Down

0 comments on commit 7e46c96

Please sign in to comment.