Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion src/main/php/xp/compiler/CompileRunner.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static function main(array $args) {
$in= $out= '-';
$quiet= false;
$augment= [];
$ext= \xp::CLASS_FILE_EXT;
for ($i= 0; $i < sizeof($args); $i++) {
if ('-t' === $args[$i]) {
$target= $args[++$i];
Expand All @@ -78,6 +79,8 @@ public static function main(array $args) {
$out= $args[++$i];
$in= array_slice($args, $i + 1);
break;
} else if ('-e' === $args[$i]) {
$ext= $args[++$i];
} else if ('-n' === $args[$i]) {
$out= null;
$in= array_slice($args, $i + 1);
Expand All @@ -98,7 +101,7 @@ public static function main(array $args) {
}

$input= Input::newInstance($in);
$output= Output::newInstance($out);
$output= Output::newInstance($out)->using($ext);

$t= new Timer();
$total= $errors= 0;
Expand Down
12 changes: 12 additions & 0 deletions src/main/php/xp/compiler/Output.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use util\cmd\Console;

abstract class Output {
protected $extension= \xp::CLASS_FILE_EXT;

/**
* Returns output from the command line argument
Expand All @@ -24,6 +25,17 @@ public static function newInstance($arg) {
}
}

/**
* Change file extension, which defaults to `xp::CLASS_FILE_EXT`.
*
* @param string $extension
* @return self
*/
public function using($extension) {
$this->extension= '.' === $extension[0] ? $extension : '.'.$extension;
return $this;
}

/**
* Returns the target for a given input
*
Expand Down
16 changes: 11 additions & 5 deletions src/main/php/xp/compiler/ToArchive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ public function __construct($file) {
* @return io.streams.OutputStream
*/
public function target($name) {
return new class($this->archive, $name) implements OutputStream {
private static $replace= [DIRECTORY_SEPARATOR => '/', '.php' => \xp::CLASS_FILE_EXT];
return new class($this->archive, $name, $this->extension) implements OutputStream {
private $archive, $name, $replace;
private $bytes= '';
private $archive, $name;

public function __construct($archive, $name) { $this->archive= $archive; $this->name= $name; }
public function __construct($archive, $name, $extension) {
$this->archive= $archive;
$this->name= $name;
$this->replace= [DIRECTORY_SEPARATOR => '/', '.php' => $extension];
}

public function write($bytes) { $this->bytes.= $bytes; }

public function flush() { }
public function close() { $this->archive->addBytes(strtr($this->name, self::$replace), $this->bytes); }

public function close() { $this->archive->addBytes(strtr($this->name, $this->replace), $this->bytes); }
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/php/xp/compiler/ToFolder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function __construct($folder) {
*/
public function target($name) {
if ('-' === $name) {
$f= new File($this->folder, 'out'.\xp::CLASS_FILE_EXT);
$f= new File($this->folder, 'out'.$this->extension);
} else {
$f= new File($this->folder, str_replace('.php', \xp::CLASS_FILE_EXT, $name));
$f= new File($this->folder, str_replace('.php', $this->extension, $name));
}
$this->ensure($f->path);
return $f->out();
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/lang/ast/unittest/cli/ToFolderTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function can_create() {

#[Test]
public function dash_special_case() {
with ((new ToFolder($this->folder))->target('-'), function($out) {
with ((new ToFolder($this->folder))->using('.php')->target('-'), function($out) {
$out->write('<?php ...');
});
Assert::true((new File($this->folder, 'out'.\xp::CLASS_FILE_EXT))->exists());
Assert::true((new File($this->folder, 'out.php'))->exists());
}

#[Test]
Expand Down