Skip to content

Commit

Permalink
Merge branch 'project-attr-from-file'
Browse files Browse the repository at this point in the history
  • Loading branch information
domm committed Sep 23, 2021
2 parents 795ca69 + 3bd4630 commit b1d0602
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ App::TimeTracker - time tracking for impatient and lazy command line lovers

# VERSION

version 3.009
version 3.010

# SYNOPSIS

Expand Down
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is generated by Dist::Zilla::Plugin::CPANFile v6.017
# This file is generated by Dist::Zilla::Plugin::CPANFile v6.024
# Do not edit this file directly. To change prereqs, edit the `dist.ini` file.

requires "Carp" => "0";
Expand Down
21 changes: 19 additions & 2 deletions lib/App/TimeTracker/Command/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,13 @@ sub cmd_init {
exit;
}

my @dirs = $cwd->dir_list;
my $project = $dirs[-1];
my $project;
if( $self->has_current_project ) {
$project = $self->_current_project;
} else {
my @dirs = $cwd->dir_list;
$project = $dirs[-1];
}
my $fh = $cwd->file('.tracker.json')->openw;
say $fh <<EOCONFIG;
{
Expand Down Expand Up @@ -704,6 +709,18 @@ sub _load_attribs_recalc_trackfile {
);
}

sub _load_attribs_init {
my ( $class, $meta ) = @_;
$meta->add_attribute(
'project' => {
isa => 'Str',
is => 'ro',
documentation => 'Project name to initialize',
lazy_build => 1,
}
);
}

sub _build_from {
my $self = shift;
if ( my $last = $self->last ) {
Expand Down
64 changes: 29 additions & 35 deletions lib/App/TimeTracker/Proto.pm
Original file line number Diff line number Diff line change
Expand Up @@ -169,51 +169,45 @@ sub load_config {
$opt_parser->getoptions( "project=s" => \$project );

if ( defined $project ) {
$self->project($project);
if ( my $project_config = $projects->{$project} ) {
$self->project($project);
$dir = Path::Class::Dir->new($project_config);
}
else {
say "Unknown project: $project";
$self->project($project);
$dir = Path::Class::Dir->new( '/ttfake', $project );
}
}

my $try = 0;
$dir = $dir->absolute;
WALKUP: while ( $try++ < 30 ) {
my $config_file = $dir->file('.tracker.json');
my $this_config;
if ( -e $config_file ) {
push( @used_config_files, $config_file->stringify );
$this_config = $self->slurp_config($config_file);
$config = merge( $config, $this_config );

my @path = $config_file->parent->dir_list;
my $project = $path[-1];
$cfl->{$project} = $config_file->stringify;
$self->project($project)
unless $self->has_project;

}
last WALKUP if $dir->parent eq $dir;

if ( my $parent = $this_config->{'parent'} ) {
if ( $projects->{$parent} ) {
$dir = Path::Class::file( $projects->{$parent} )->parent;
if ($dir) {
my $try = 0;
$dir = $dir->absolute;
WALKUP: while ( $try++ < 30 ) {
my $config_file = $dir->file('.tracker.json');
my $this_config;
if ( -e $config_file ) {
push( @used_config_files, $config_file->stringify );
$this_config = $self->slurp_config($config_file);
$config = merge( $config, $this_config );

my @path = $config_file->parent->dir_list;
my $project = exists $this_config->{project} ? $this_config->{project} : $path[-1];
$cfl->{$project} = $config_file->stringify;
$self->project($project)
unless $self->has_project;
}
last WALKUP if $dir->parent eq $dir;

if ( my $parent = $this_config->{'parent'} ) {
if ( $projects->{$parent} ) {
$dir = Path::Class::file( $projects->{$parent} )->parent;
}
else {
$dir = $dir->parent;
say
"Cannot find project >$parent< that's specified as a parent in $config_file";
}
}
else {
$dir = $dir->parent;
say
"Cannot find project >$parent< that's specified as a parent in $config_file";
}
}
else {
$dir = $dir->parent;
}
}

$self->_write_config_file_locations($cfl);

if ( -e $self->global_config_file ) {
Expand Down
90 changes: 90 additions & 0 deletions t/Command/core.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ my $tmp = testlib::Fixtures::setup_tempdir;
my $home = $tmp->subdir('.TimeTracker');
$tmp->subdir('some_project')->mkpath;
$tmp->subdir('other_project')->mkpath;
$tmp->subdir('project_name_auto')->mkpath;
$tmp->subdir('project_name_custom')->mkpath;
my $p = App::TimeTracker::Proto->new( home => $home );
my $now = DateTime->now;
$now->set_time_zone('local');
Expand Down Expand Up @@ -42,6 +44,94 @@ my $tracker_dir = $home->subdir( $now->year, sprintf( "%02d", $now->month ) );

}

{ # init (setting project name)
file_not_exists_ok( $tmp->file( 'project_name_auto', '.tracker.json' ) );
file_not_exists_ok( $tmp->file( 'project_name_custom', '.tracker.json' ) );

@ARGV = ('init');
my $class = $p->setup_class( {} );

{
my $p = App::TimeTracker::Proto->new( home => $home );
# _current_project not set
my $t = $class->name->new( home => $home, config => {} );
trap { $t->cmd_init( $tmp->subdir('project_name_auto') ) };

file_exists_ok( $tmp->file( 'project_name_auto', '.tracker.json' ) );
my $config = $p->load_config( $tmp->subdir(qw(project_name_auto)) );
is $config->{project}, 'project_name_auto', 'automatic project name';
is $p->project, 'project_name_auto', 'project attribute set correctly';
}

{
my $p = App::TimeTracker::Proto->new( home => $home );
# _current_project is set
my $t = $class->name->new( home => $home, config => {}, _current_project => 'my-custom-project' );
trap { $t->cmd_init( $tmp->subdir('project_name_custom') ) };

file_exists_ok( $tmp->file( 'project_name_custom', '.tracker.json' ) );
my $config = $p->load_config( $tmp->subdir(qw(project_name_custom)) );
is $config->{project}, 'my-custom-project', 'custom project name';
is $p->project, 'my-custom-project', 'project attribute set from file';
}

}

{ # init (setting up project tree)
@ARGV = ('init');
my $class = $p->setup_class( {} );

my $project_dir = $tmp->subdir('project-top');
my $subproj0_dir = $project_dir->subdir('subproj0');
$subproj0_dir->mkpath;
my $subproj1_dir = $project_dir->subdir('subproj1');
$subproj1_dir->mkpath;

{
my $p = App::TimeTracker::Proto->new( home => $home );
# _current_project not set
{
my $t = $class->name->new( home => $home, config => {}, _current_project => 'top' );
trap { $t->cmd_init( $project_dir ) };
}

{
my $t = $class->name->new( home => $home, config => {}, _current_project => 'sub' );
trap { $t->cmd_init( $subproj0_dir ) };
}

{
my $t = $class->name->new( home => $home, config => {} );
trap { $t->cmd_init( $subproj1_dir ) };
# set config without "project" key so that last component of path
# must be used
$subproj1_dir->file('.tracker.json')->spew("{}");
}

my $tracker_files = superhashof({
'top' => "" . $project_dir->file('.tracker.json'),
'sub' => "" . $subproj0_dir->file('.tracker.json'),
'subproj1' => "" . $subproj1_dir->file('.tracker.json'),
});
my $projects_data;

$projects_data = $p->slurp_projects;
cmp_deeply $projects_data, $tracker_files, 'after init of all projects';

$p->load_config( $project_dir );
$projects_data = $p->slurp_projects;
cmp_deeply $projects_data, $tracker_files, 'after loading top project';

$p->load_config( $subproj0_dir );
$projects_data = $p->slurp_projects;
cmp_deeply $projects_data, $tracker_files, 'after loading sub project';

$p->load_config( $subproj1_dir );
$projects_data = $p->slurp_projects;
cmp_deeply $projects_data, $tracker_files, 'after loading subproj1 project';
}
}

my $c1 = $p->load_config( $tmp->subdir(qw(some_project)) );

{ # start
Expand Down

0 comments on commit b1d0602

Please sign in to comment.