Skip to content

Commit

Permalink
Merge pull request #12 from launchrctl/10-copy_versioned_only_files
Browse files Browse the repository at this point in the history
add option to copy only versioned files from source dir
  • Loading branch information
iignatevich authored Aug 23, 2023
2 parents e0e0550 + 3eed6c8 commit c76f828
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
26 changes: 8 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ launchr compose [options]
Where options are:
* -w, --working-dir : The directory where temporary files should be stored during the
composition process. Default is the .compose/packages
* -s, --skip-not-versioned : Skip not versioned files from source directory (git only)

example usage - `launchr compose -w=./folder/something`
example usage - `launchr compose -w=./folder/something -s=1 or -s=true`

### `compose.yaml` File Format
The "compose.yaml" file is a text file that specifies the dependencies for a package, along with any necessary metadata and sources for those dependencies.
Expand All @@ -33,12 +34,9 @@ dependencies:
- name: compose-example
source:
type: git
ref: 0.0.1
ref: master
tag: 0.0.1
url: https://github.com/example/compose-example.git
# temporary basic auth (http)
auth:
name: abc
password: bbc
```
`Lock example`
Expand All @@ -50,7 +48,8 @@ packages:
source:
type: git
url: https://github.com/example/compose-dependency-example.git
ref: 0.0.1
ref: master
tag: 0.0.1
- name: compose-example-http
source:
type: http
Expand All @@ -59,11 +58,8 @@ packages:
source:
type: git
url: https://github.com/example/compose-example.git
ref: 0.0.7
# temporary basic auth
auth:
name: abc
password: bbc
ref: master
tag: 0.0.7
dependencies:
- compose-dependency-example
- compose-example-http
Expand All @@ -80,9 +76,3 @@ The composition tool fetches and installs dependencies for a package by recursiv
5. Repeat steps 1-4 for each package and its dependencies.

During this process, the composition tool keeps track of the dependencies for each package.

#TO-DO

- [ ] add concurrent download for first compose when packagist-like platform will be available.
- [ ] checksums for packages
- [ ] merging strategies
4 changes: 3 additions & 1 deletion compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (p *Plugin) OnAppInit(app launchr.App) error {
// CobraAddCommands implements launchr.CobraPlugin interface to provide compose functionality.
func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
var workingDir string
var skipNotVersioned bool
var composeCmd = &cobra.Command{
Use: "compose",
Short: "Composes filesystem (files & dirs)",
Expand All @@ -45,7 +46,7 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
c, err := compose.CreateComposer(
os.DirFS(p.wd),
p.wd,
compose.ComposerOptions{WorkingDir: workingDir},
compose.ComposerOptions{WorkingDir: workingDir, SkipNotVersioned: skipNotVersioned},
p.k,
)
if err != nil {
Expand All @@ -57,6 +58,7 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
}

composeCmd.Flags().StringVarP(&workingDir, "working-dir", "w", ".compose/packages", "Working directory for temp files")
composeCmd.Flags().BoolVarP(&skipNotVersioned, "skip-not-versioned", "s", false, "Skip not versioned files from source directory (git only)")
rootCmd.AddCommand(composeCmd)
return nil
}
64 changes: 58 additions & 6 deletions compose/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stevenle/topsort"
)

const (
// DependencyRoot is a dependencies graph main node
DependencyRoot = "root"
gitPrefix = ".git"
)

var excludedFolders = map[string]struct{}{".idea": {}, ".compose": {}}
var excludedFiles = map[string]struct{}{composeFile: {}, composeLock: {}}

// Builder struct, provides methods to merge packages into build
type Builder struct {
platformDir string
targetDir string
sourcedir string
graph topsort.Graph
platformDir string
targetDir string
sourcedir string
skipNotVersioned bool
graph topsort.Graph
}

type fsEntry struct {
Expand All @@ -33,8 +38,34 @@ type fsEntry struct {
Excluded bool
}

func createBuilder(platformDir, targetDir, sourcedir string, graph topsort.Graph) *Builder {
return &Builder{platformDir, targetDir, sourcedir, graph}
func createBuilder(platformDir, targetDir, sourcedir string, skipNotVersioned bool, graph topsort.Graph) *Builder {
return &Builder{platformDir, targetDir, sourcedir, skipNotVersioned, graph}
}

func getVersionedMap(gitDir string) (map[string]bool, error) {
versionedFiles := make(map[string]bool)
repo, err := git.PlainOpen(gitDir)
if err != nil {
return versionedFiles, err
}
head, err := repo.Head()
if err != nil {
return versionedFiles, err
}

commit, _ := repo.CommitObject(head.Hash())
tree, _ := commit.Tree()
tree.Files().ForEach(func(f *object.File) error {
dir := filepath.Dir(f.Name)
if _, ok := versionedFiles[dir]; !ok {
versionedFiles[dir] = true
}

versionedFiles[f.Name] = true
return nil
})

return versionedFiles, nil
}

func (b *Builder) build() error {
Expand All @@ -43,6 +74,15 @@ func (b *Builder) build() error {
return err
}

versionedMap := make(map[string]bool)
checkVersioned := b.skipNotVersioned
if checkVersioned {
versionedMap, err = getVersionedMap(b.platformDir)
if err != nil {
checkVersioned = false
}
}

items, _ := b.graph.TopSort(DependencyRoot)
baseFs := os.DirFS(b.platformDir)

Expand All @@ -66,6 +106,13 @@ func (b *Builder) build() error {
}
}

// Add .git folder into entriesTree whenever checkversioned or not
if checkVersioned && !strings.HasPrefix(path, gitPrefix) {
if _, ok := versionedMap[path]; !ok {
return nil
}
}

finfo, _ := d.Info()
entry := &fsEntry{Prefix: b.platformDir, Path: path, Entry: finfo, Excluded: false}
entriesTree = append(entriesTree, entry)
Expand All @@ -88,6 +135,11 @@ func (b *Builder) build() error {
return err
}

// Skip .git folder from packages
if strings.HasPrefix(path, gitPrefix) {
return nil
}

finfo, _ := d.Info()
entry := &fsEntry{Prefix: pkgPath, Path: path, Entry: finfo, Excluded: false}

Expand Down
5 changes: 3 additions & 2 deletions compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Composer struct {

// ComposerOptions - list of possible composer options
type ComposerOptions struct {
WorkingDir string
WorkingDir string
SkipNotVersioned bool
}

// CreateComposer instance
Expand Down Expand Up @@ -76,7 +77,7 @@ func (c *Composer) RunInstall() error {

// build graph and merge packages in build dir
graph := buildDependenciesGraph(lock.Packages)
builder := createBuilder(c.pwd, buildDir, packagesDir, *graph)
builder := createBuilder(c.pwd, buildDir, packagesDir, c.options.SkipNotVersioned, *graph)
return builder.build()
}

Expand Down

0 comments on commit c76f828

Please sign in to comment.