Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyjul committed May 5, 2023
1 parent 2bd4fa9 commit 5adf56f
Show file tree
Hide file tree
Showing 14 changed files with 820 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.n
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,40 @@ assetman

Haxe library used to generate ninja file.

The goal is to scan the directories, and using the rules, generate a ninja.build

Rules
-----

You can create rules, that will be reused later

rule('convert2').command("cp $in $out");
rule('convert').command("cp $in $out");
rule('atlas').command("cat $in > $name");

`$in` and `$out` are default from ninja, but here, `$name` is a variable that will be set later when rule is used

Patterns
--------

Pattern are use to apply rule to file sets

var pattern = '**/*.png';
// build [email protected]: convert2 ../src/foo.psd
single(pattern).to("[email protected]").usingRule('convert2');
// build foo.png: convert ../src/foo.psd
single(pattern).toExt('.png').usingRule('convert');
var atlasName = 'atlas';
// build atlas.png atlas.csv: atlas foo.png [email protected]
// name = atlas
bundle('images/*.png')
.fromBuild(true)
.to([atlasName + '.png', atlasName + '.csv'])
.assign('name', atlasName)
.usingRule('atlas');


Acknowledgment
--------------

This library was heavily inspired by https://github.com/tylorr/assetman
36 changes: 36 additions & 0 deletions example/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import assetman.Builder;

class CustomBuilder extends Builder {
public function new() {
super();
}

function configure() {
rule('convert2').command("cp $in $out");
rule('convert').command("cp $in $out");
rule('atlas').command("cat $in > $name.png");
var pattern = '**/*.png';
// build [email protected]: convert2 ../src/foo.psd
single(pattern).to("[email protected]").usingRule('convert2');
// build foo.png: convert ../src/foo.psd
single(pattern).toExt('.png').usingRule('convert');
var atlasName = 'atlas';
// build atlas.png atlas.csv: atlas foo.png [email protected]
// name = atlas
bundle('images/*.png')
.fromBuild(true)
.to([atlasName + '.png'])
.assign('name', atlasName)
.usingRule('atlas');
}
}

class Main {


static function main() {
var builder = new CustomBuilder();
builder.generate("../test");
}
}
11 changes: 11 additions & 0 deletions example/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Neko
-main Main
-cp src
-lib assetman
-neko assetman.n

--next

-main CompareEcho
-lib assetman
-neko compare_echo.n
73 changes: 73 additions & 0 deletions src/CompareEcho.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

import sys.FileSystem;
import hx.files.Dir;
import haxe.io.Path;

class CompareEcho {


static function main() {

var args = Sys.args();

var current_directory : String;
if(args.length >= 3) {
current_directory = args[2];
} else {
current_directory = FileSystem.absolutePath("");
}

trace( Sys.args().join('\n') );

// construct search patterns
var patterns = args[0].split(' ').map(function(a){return a;});
var pattern = patterns.length > 1 ? '{' + patterns.join(',') + '}' : patterns[0];

trace('Pattern ${pattern} with cw');
// collect files that match pattern
var dir = Dir.of(current_directory);
var files = dir.findFiles(pattern).map(
function(a) {
return relativePath( FileSystem.absolutePath(current_directory), a.path.getAbsolutePath() );
});
files.sort( Reflect.compare );
var files_as_text = files.join('\n');

trace(files_as_text);

// update file list in output if they don't match
var outpath = args[1];
var write_file : Bool = true;

if( sys.FileSystem.exists( outpath )){
var oldfiles = sys.io.File.getContent( outpath );
write_file = oldfiles != files_as_text;
}

if(write_file){
sys.io.File.saveContent(outpath, files_as_text);
}
}

static function relativePath(relativeTo: String, path: String) {
// make both absolute
path = Path.removeTrailingSlashes(FileSystem.absolutePath(path));
relativeTo = Path.removeTrailingSlashes(FileSystem.absolutePath(relativeTo));
var aPath = path.split('/');
var aRelativeTo = relativeTo.split('/');
// find shared part of path
var matchesUpToIndex = 0;

for(i in 0...aRelativeTo.length) {
if(aPath[i] == aRelativeTo[i]) {
matchesUpToIndex = i;
} else {
break;
}
}

return [for(_ in 0...(aRelativeTo.length - 1) - matchesUpToIndex) '..']
.concat(aPath.slice(matchesUpToIndex + 1))
.join('/');
}
}
Loading

0 comments on commit 5adf56f

Please sign in to comment.