Chandra

 view release on metacpan or  search on metacpan

lib/Chandra/Pack.pm  view on Meta::CPAN

our $VERSION = '0.29';

use File::Raw qw(import);
use Cwd ();

# ── Configuration Storage ────────────────────────────────────────────

our %CONFIG = (
    # macOS
    identity        => '-',          # ad-hoc signing by default
    apple_id        => undef,
    team_id         => undef,
    notary_keychain => undef,
    
    # Windows (future)
    sign_cert       => undef,
    sign_password   => undef,
);

sub config {
    my $class = shift;
    
    # Getter: config() or config('key')
    if (@_ == 0) {
        return %CONFIG;
    }
    if (@_ == 1 && !ref $_[0]) {
        return $CONFIG{$_[0]};
    }
    
    # Setter: config(key => val, ...)
    my %args = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
    
    # Load from env vars if not provided
    $args{identity}        //= $ENV{CHANDRA_IDENTITY};
    $args{apple_id}        //= $ENV{CHANDRA_APPLE_ID};
    $args{team_id}         //= $ENV{CHANDRA_TEAM_ID};
    $args{notary_keychain} //= $ENV{CHANDRA_NOTARY_KEYCHAIN};
    $args{sign_cert}       //= $ENV{CHANDRA_SIGN_CERT};
    $args{sign_password}   //= $ENV{CHANDRA_SIGN_PASSWORD};
    
    for my $key (keys %args) {
        if (exists $CONFIG{$key}) {
            $CONFIG{$key} = $args{$key};
        } else {
            Carp::carp("Unknown config key: $key");
        }
    }
    
    # Save to file if requested
    if (delete $args{save}) {
        _save_config();
    }
    
    return $class;
}

sub _config_file {
    my $home = $ENV{HOME};
    if (!$home && $^O ne 'MSWin32') {
        $home = (getpwuid($<))[7];
    }
    $home ||= $ENV{USERPROFILE} || '.';
    return file_join($home, '.chandra', 'pack.conf');
}

sub _load_config {
    my $file = _config_file();
    return unless file_is_file($file);
    my $content = file_slurp($file);
    for my $line (split /\n/, $content) {
        next if $line =~ /^\s*#/ || $line =~ /^\s*$/;
        if ($line =~ /^(\w+)\s*=\s*(.*)$/) {
            $CONFIG{$1} = $2 if exists $CONFIG{$1};
        }
    }
}

sub _save_config {
    my $file = _config_file();
    my $dir = file_dirname($file);
    file_mkpath($dir) unless file_is_dir($dir);
    my $content = "# Chandra::Pack configuration\n";
    for my $key (sort keys %CONFIG) {
        next unless defined $CONFIG{$key};
        $content .= "$key = $CONFIG{$key}\n";
    }
    file_spew($file, $content);
    chmod 0600, $file;  # Protect credentials
}

# Load config on module load
_load_config();

sub new {
    my ($class, %args) = @_;
    Carp::croak("'script' is required") unless $args{script};
    Carp::croak("Script '$args{script}' not found") unless file_exists($args{script});
    Carp::croak("Script '$args{script}' is not a file") unless file_is_file($args{script});

    my $name = $args{name} || _name_from_script($args{script});
    bless {
        script     => Cwd::abs_path($args{script}),
        name       => $name,
        version    => $args{version}    || '0.0.1',
        icon       => $args{icon},
        assets     => $args{assets},
        output     => $args{output}     || '.',
        platform   => $args{platform}   || _detect_platform(),
        identifier => $args{identifier} || _default_identifier($name),
        perl       => $args{perl}       || $^X,
        include    => $args{include}    || [],
        exclude    => $args{exclude}    || [],
        distribute => $args{distribute} || 0,
        _deps      => undef,
    }, $class;
}

# ── Accessors ────────────────────────────────────────────────────────

sub script     { $_[0]->{script} }



( run in 1.228 second using v1.01-cache-2.11-cpan-995e09ba956 )