Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnsholt/458738 to your computer and use it in GitHub Desktop.
Save arnsholt/458738 to your computer and use it in GitHub Desktop.
From 688c9cd76289ce72ce37ace500f33a2b001a2c9c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arne=20Skj=C3=A6rholt?= <arnsholt@gmail.com>
Date: Wed, 30 Jun 2010 17:41:08 +0200
Subject: [PATCH] Only print unique directories, and not their prefixes. v2.
---
ufo | 20 ++++++++++++++++++--
1 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/ufo b/ufo
index 3912755..0db8817 100755
--- a/ufo
+++ b/ufo
@@ -143,15 +143,31 @@ sub directory-of($file) {
$file.subst(/ '/' <-[/]>*? $ /, '');
}
+sub prefix($prefix, $str) {
+ my $index = $str.index($prefix);
+ return defined $index && $index == 0;
+}
+
sub write-install($extension?) {
+ my %dirs;
+ # Find unique directory names and filter out only those who are not
+ # prefixes of other directories.
+ %dirs{$_} = 1 for @sources.map: { directory-of($^a) }
+ # Lexicographic sort places prefixes before the string they are a
+ # prefix of.
+ my $prev = '';
+ for %dirs.keys.sort -> $cur {
+ $makefile.say: "\tmkdir -p ~/.perl6/$cur" if not prefix($prev, $cur);
+ $prev = $cur;
+ }
+ $makefile.say: "\tmkdir -p ~/.perl6/$prev";
+
for @sources -> $s {
my $file = defined $extension
?? $s.subst(rx{\.pm6?$}, '.' ~ $extension)
!! $s;
# Can't use 'install -D' like we originally did, because Mac OS X
# has that flag as '-d'.
- my $directory = directory-of($file);
- $makefile.say("\tmkdir -p ~/.perl6/$directory");
$makefile.say("\tinstall $file ~/.perl6/$file");
}
}
--
1.7.0.4
MINI-CHALLENGE
==============
Current 'install' target output looks like this:
install: all
mkdir -p ~/.perl6/lib/Form
install lib/Form/TextFormatting.pir ~/.perl6/lib/Form/TextFormatting.pir
mkdir -p ~/.perl6/lib/Form
install lib/Form/NumberFormatting.pir ~/.perl6/lib/Form/NumberFormatting.pir
mkdir -p ~/.perl6/lib/Form
install lib/Form/Field.pir ~/.perl6/lib/Form/Field.pir
mkdir -p ~/.perl6/lib/Form
install lib/Form/Actions.pir ~/.perl6/lib/Form/Actions.pir
mkdir -p ~/.perl6/lib/Form
install lib/Form/Grammar.pir ~/.perl6/lib/Form/Grammar.pir
mkdir -p ~/.perl6/lib
install lib/Form.pir ~/.perl6/lib/Form.pir
mkdir -p ~/.perl6/lib
install lib/Test.pir ~/.perl6/lib/Test.pir
Only the first 'mkdir' is necessary. Removing reduntant identical 'mkdir's would
be easy. Removing those that only create substrings of already created paths might
or might not be as trivial. I'd do the latter iff it turns out that the code for it
looks nice. :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment