Skip to content
This repository was archived by the owner on Dec 21, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
674d31b
Quick fix $HOME for win32 platform
ibolmo Oct 15, 2010
eba9209
Can pass in components to exclude from the build (like Depender) for …
ryanflorence Oct 28, 2010
c95c6b8
An infinite loop in the solution part of the dependence is generated.
holyshared Oct 29, 2010
e37f08a
Merge remote branch 'remotes/ibolmo/master'
holyshared Oct 30, 2010
0153d90
sources by pattern
slik Nov 29, 2010
ec7e61c
merged
slik Nov 29, 2010
d6159c5
bfglob is a private method now
slik Nov 30, 2010
bf02a16
Added the ability to read the description for a file from the source-…
Dec 17, 2010
99b9f6b
Restore ability to end YAML comment with dashes
appden Mar 3, 2011
4140a4a
Fix bug where file object was missing name property
appden Mar 3, 2011
d6f88a6
Factored out some code in build into separate methods: resolve_files …
appden Mar 3, 2011
9b846a5
The bug that the dependence of the file cannot be correctly acquired …
holyshared Mar 26, 2011
1674190
Merge remote branch 'remotes/rpflorence/master'
holyshared Mar 26, 2011
0ffffc6
Merge remote branch 'remotes/thomasd/master'
holyshared Mar 26, 2011
4bb07ad
The change of https://github.com/slik/packager.git is merged.
holyshared Mar 26, 2011
ff9b166
The change of https://github.com/appden/packager.git is merged.
holyshared Mar 26, 2011
ce39df8
The distinction from isset() to empty() because the error competes at…
holyshared Mar 26, 2011
f119a49
Because the output message cannot be read by other programs, the meth…
holyshared Mar 28, 2011
9f585fb
It is corrected that an extra output is included.
holyshared Mar 28, 2011
ad66d9c
(broken) refactoring of packager into more modular approach.
ibolmo Apr 25, 2011
cd278ec
An interim commit. Quick pass and doesn't seem that anything else (wi…
ibolmo Apr 25, 2011
de2dc42
Added phpunit test_packager.php.
ibolmo Apr 25, 2011
a037178
Separated tests into two files. Fixed a bug with normalize_requires.
ibolmo Apr 25, 2011
c9adde3
Passing tests (so far).
ibolmo Apr 25, 2011
b3dfc8a
Fixing get_all_files. Should return all the files for all packages in…
ibolmo Apr 25, 2011
2fc967c
Moved Package to its own file. At the moment it's in root directory, …
ibolmo Apr 25, 2011
f0903d2
Moved Source specific code into a new Source class. Required if I'm g…
ibolmo Apr 26, 2011
1bce00b
Small optimization and added option for added a source to return the …
ibolmo Apr 27, 2011
d848d8e
Stripped down and cleaned up the parsing. Packager next.. this will b…
ibolmo Apr 27, 2011
1deeaed
All Class files should have a capitalized filename.
ibolmo Apr 27, 2011
4547ea7
interim. getting late.
ibolmo Apr 27, 2011
dc71b9d
Some more work done on the Source. Now working on the graph on the Pa…
ibolmo Apr 28, 2011
eb4a589
Added the add_component method.
ibolmo Apr 29, 2011
6b157d9
Small fix and removing notes
ibolmo Apr 29, 2011
8e83c2e
Forgot to include the Source#get_package_name
ibolmo Apr 29, 2011
ef6dcb4
Another small fix. list doesn't work with indexed arrays.
ibolmo Apr 29, 2011
4228d60
Added a component class. I may remove the class later on.
ibolmo Apr 30, 2011
da9578e
dirname(__FILE__) to __DIR__
ibolmo Apr 30, 2011
7dba104
requiring once.. fixed add_component and add_dependency
ibolmo May 2, 2011
06526d8
Working tests.
ibolmo May 2, 2011
9b43497
Added more coverage and fixed ksort requirement.
ibolmo May 4, 2011
e30bb25
Removed add_dependency since now that is maintained by source. About …
ibolmo May 5, 2011
9fed39a
Working Packager::build
ibolmo May 5, 2011
3ec1d5a
Working Source::build
ibolmo May 5, 2011
c7f6fff
All test pass. Added a todo for overwriting packages.
ibolmo May 5, 2011
f1402c3
Fixed redundant deps.
ibolmo May 6, 2011
bc6b03a
Added Packager.strip_blocks. Decided that it's not the build function
ibolmo May 6, 2011
070e4db
Warning that there is no yml, rather than requiring it.
ibolmo May 7, 2011
846479b
Fixing a cpass by reference deprecation, and err in previous commit (…
ibolmo May 7, 2011
ea7b953
A couple of key changes. get_required_for_source now returns in required
ibolmo May 7, 2011
cff1fce
Edited README.md via GitHub
ibolmo May 8, 2011
831a41f
Edited README.md via GitHub
ibolmo May 8, 2011
afe0486
The easy part. register, unregister, and list now uses 2.0 api.
ibolmo May 18, 2011
8165609
Added support to remove packages, sources, and components. Did a bit …
ibolmo May 19, 2011
5f410c6
Working CLI but broken for wildcards.
ibolmo May 20, 2011
048ed80
Merge remote branch 'remotes/origin/2.0-cli'
holyshared Sep 25, 2011
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions Package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

require_once __DIR__ . '/helpers/yaml.php';
require_once __DIR__ . '/Source.php';

class Package
{
protected $sources = array();

public function __construct($package_path = '')
{
if ($package_path){
$this->path = $this->resolve_path($package_path);
$this->root_dir = dirname($this->path);
$this->parse($this->path);
}
}

public function __toString()
{
return $this->get_name();
}

static function decode($path){
return preg_match('/\.json$/', $path) ? json_decode(file_get_contents($path), true) : YAML::decode_file($path);
}

static function glob($path, $pattern = '*', $flags = 0, $depth = 0){
$matches = array();
$folders = array(rtrim($path, DIRECTORY_SEPARATOR));

while ($folder = array_shift($folders)){
$matches = array_merge($matches, glob($folder.DIRECTORY_SEPARATOR.$pattern, $flags));

if (!$depth) continue;

$moreFolders = glob($folder.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
$depth = ($depth < -1) ? -1: $depth + count($moreFolders) - 2;
$folders = array_merge($folders, $moreFolders);
}
return $matches;
}

public function add_source($source_path = '')
{
if (!is_a($source_path, 'Source')) $source_path = new Source($this->get_name(), $source_path);
$this->sources[] = $source_path;
}

public function get_name()
{
return $this->name;
}

public function get_path()
{
return $this->path;
}

public function get_sources()
{
return $this->sources;
}

public function parse($package_path)
{
$package = self::decode($package_path);

foreach ($package as $key => $value){
$method = 'parse_' . strtolower($key);
if (is_callable(array($this, $method))) $this->$method($value);
}
}

public function parse_name($name)
{
$this->set_name($name);
}

public function parse_sources($sources)
{
# todo(ibolmo): 5, should be a class option.
if (is_string($sources)) $sources = self::glob($this->path, $sources, 0, 5);
foreach ($sources as $source) $this->add_source($this->root_dir . '/' . $source);
}

public function set_name($name)
{
$this->name = $name;
return $this;
}

public function resolve_path($path){
if (!is_dir($path) && file_exists($path)) return $path;

$pathinfo = pathinfo($path);
$path = $pathinfo['dirname'] . '/' . $pathinfo['basename'] . '/';

if (file_exists($path . 'package.yml')) return $path . 'package.yml';
if (file_exists($path . 'package.yaml')) return $path . 'package.yaml';
if (file_exists($path . 'package.json')) return $path . 'package.json';

throw new Exception("package.(ya?ml|json) not found in $path.");
}
}
174 changes: 174 additions & 0 deletions Packager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

# todo(ibolmo): packages with the same name cause an endless loop

require_once __DIR__ . '/Package.php';

class Packager {

static $instance;

protected $sources = array();
protected $generators = array();
protected $keys = array();

public function __construct($package_paths = '')
{
$this->configure();
if ($package_paths) foreach ((array) $package_paths as $package) $this->add_package($package);
}

static function get_instance()
{
if (!self::$instance) self::$instance = new Packager();
return self::$instance;
}

static function strip_blocks($code, $blocks)
{
foreach ((array) $blocks as $block){
$code = preg_replace_callback("%(/[/*])\s*<$block>(.*?)</$block>(?:\s*\*/)?%s", function($matches){
return (strpos($matches[2], ($matches[1] == "//") ? "\n" : "*/") === false) ? $matches[2] : "";
}, $code);
}
return $code;
}

public function add_component(Source $source, $component)
{
$index = $this->get_source_index($source);
if ($index < 0) $index = $this->add_source($source);

foreach ($this->generators as $name => $callback){
$key = call_user_func($callback, $source, $component);
$this->set_key($key, $index, $name);
}

return $index;
}

public function add_generator($name, $callback)
{
$this->generators[$name] = $callback;
}

public function add_package($package)
{
if (!is_a($package, 'Package')) $package = new Package($package);
$this->packages[$package->get_name()] = $package;
}

public function add_source(Source $source)
{
$index = array_push($this->sources, $source) - 1;
return $this->keys[$source->get_name()] = $index;
}

public function build(Source $source)
{
$build = array_map(function($source){
return $source->get_code();
}, $this->get_required_for_source($source));
$build[] = $source->get_code();
return implode('', $build);
}

public function configure()
{
$this->add_generator('component name', function(Source $source, $component){
return $component;
});

$this->add_generator('package and source name', function(Source $source, $component){
return sprintf('%s/%s', $source->get_package_name(), $source->get_name());
});

$this->add_generator('package and component name', function(Source $source, $component){
return sprintf('%s/%s', $source->get_package_name(), $component);
});
}

public function get_packages()
{
return $this->packages;
}

public function get_source_by_name($name)
{
$index = $this->get_source_index($name);
if ($index > -1) return $this->sources[$index];

$this->warn("Could not find source '$name'.");
return null;
}

public function get_source_index($key)
{
if ($key instanceof Source) $key = $key->get_name();
return isset($this->keys[$key]) ? $this->keys[$key] : -1;
}

public function get_sources()
{
return $this->sources;
}

public function get_required_for_source(Source $source, &$required = null)
{
$return = false;
if (!$required){
$return = true;
$required = array();
}

foreach ($source->get_requires() as $require){
if (!($require instanceof Source)) $require = $this->get_source_by_name($require);
if (!$require) continue;
if ($require->has_requires()) $this->get_required_for_source($require, $required);
}
foreach ($source->get_requires() as $require){
if (!($require instanceof Source)) $require = $this->get_source_by_name($require);
if (!$require) continue;
if (!in_array($require, $required)) $required[] = $require;
}

if ($return) return $required;
}

public function remove_component(Source $source, $component)
{
foreach ($this->generators as $name => $callback){
$key = call_user_func($callback, $source, $component);
unset($this->keys[$key]);
}
}

public function remove_package($package_name)
{
if ($package_name instanceof Package) $package_name = $package_name->get_name();
if (!isset($this->packages[$package_name])) return false;
foreach($this->packages[$package_name]->get_sources() as $source) $this->remove_source($source);
unset($this->packages[$package_name]);
return true;
}

public function remove_source(Source $source)
{
$name = $source->get_name();
$index = $this->keys[$name];
foreach ($source->get_provides() as $component) $this->remove_component($source, $component);
unset($this->keys[$name], $this->sources[$index]);
}

protected function set_key($key, $index, $generator_name)
{
if (isset($this->keys[$key])) $this->warn("Generator '$generator_name' set component key '$key'.");
$this->keys[$key] = $index;
}

protected function warn($message)
{
# todo(ibolmo): log mixin
}

}
Loading