Skip to content

Commit

Permalink
hpack support of various kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa McHale committed Nov 28, 2017
1 parent 2be946a commit 94981c3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ bench:


packages:
@rm -rf lens-* idris-lens dhall-* language-lua-* purescript-matryoshka
@rm -rf lens-* idris-lens dhall-* language-lua-* purescript-matryoshka futhark
@git clone https://github.com/diku-dk/futhark
cd futhark && cargo run -- m . Language.Futhark.Parser.Parser Language.Futhark.Parser.Mod --hpack && cargo run -- m . Language.Futhark.TH Language.Futhark.Sin && stack build
@rm -rf futhark
@git clone https://github.com/slamdata/purescript-matryoshka.git
cd purescript-matryoshka && cargo run -- p . Matryoshka.DistributiveLaw Matryoshka.DL && npm install && bower install && npm run -s build && npm run -s test
@rm -rf purescript-matryoshka
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Build Status](https://travis-ci.org/vmchale/hask-replace.svg?branch=master)](https://travis-ci.org/vmchale/hask-replace)

`hask-replace` is a command-line tool for renaming
Haskell, Elm, and Idris modules.
Haskell, Elm, PureScript, and Idris modules.

## The Pitch

Expand All @@ -18,8 +18,7 @@ cabal new-build
```

As you can see, it's a lot less painful than whatever witchcraft you'd have to
resort to to accomplish the same thing in bash. Not only that, it works for Idris
and Elm and it detects `happy`/`alex` modules too!
resort to to accomplish the same thing in bash.

## The Anti-Pitch

Expand Down Expand Up @@ -54,7 +53,7 @@ First, install [cargo](https://rustup.rs/). Then:
You will need to use the nightly release for this to work; if in doubt run

```bash
rustup run nightly cargo install --git https://github.com/vmchale/hask-replace
$ rustup run nightly cargo install --git https://github.com/vmchale/hask-replace
```

## Performance
Expand All @@ -66,7 +65,7 @@ rustup run nightly cargo install --git https://github.com/vmchale/hask-replace

## Use

`hr` can also be used on Idris projects.
Example use:

```bash
git clone https://github.com/HuwCampbell/idris-lens.git
Expand Down
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
- [x] support for alex/happy
- [x] support for `.hsig` files
- [ ] parse cabal file to find source directories.
- [ ] work within haddocks as well.
- [ ] move a directory structure
- [ ] new module? Or new test suite perhaps?
- [ ] clean up newly-emptied directories
# Code Maintenance
- [ ] test suite for backpack
- [ ] test suite with `.hs-boot` files
- [ ] test suite with `.x` and `.out` files, etc.
- [x] test suite with hpack
13 changes: 8 additions & 5 deletions src/cabal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ named!(pub boring_line<&str, &str>,
tag!("Exposed-Modules") |
tag!("Other-Modules") |
tag!("other-modules") |
tag!("packages") |
tag!("extra-source-files") |
tag!("\"exposed-modules\":") |
tag!("\"depends\":") |
Expand Down Expand Up @@ -147,6 +148,7 @@ named!(prolegomena<&str, ()>,
tag!("Exposed-Modules:") |
tag!("Other-Modules:") |
tag!("modules =") |
tag!("packages:") |
tag!("\"exposed-modules\":") | // FIXME what if exposed-modules doesn't exist?
tag!("\"depends\":") |
tag!("\"dependencies\":")
Expand All @@ -162,9 +164,9 @@ named_args!(pub parse_all<'a>(old: &'a str, new: &'a str, old_src: &'a str, new_
a: recognize!(skip_stuff) >>
b: opt!(call!(parse_source, old_src, new_src)) >>
d: recognize!(prolegomena) >>
e: call!(parse_modules, old, new) >>
e: opt!(call!(parse_modules, old, new)) >> // hpack doesn't require an exposed-modules section
c: recognize!(opt!(skip_stuff)) >>
(join(vec![vec![a], from_vec(b), vec![d], e, vec![c]]))
(join(vec![vec![a], from_vec(b), vec![d], from_vec(e), vec![c]]))
)
) >>
b: rest_s >>
Expand All @@ -176,8 +178,8 @@ named!(step_indented<&str, &str>,
alt!(
recognize!(do_parse!(a: tag!(",") >> b: multispace >> ())) |
is_a!(" ") |
recognize!(do_parse!(a: opt!(tag!("\n")) >> b: eof!() >> ())) | // (vec![from_opt(a), b])) |
recognize!(do_parse!(c: opt!(tag!(",")) >> a: tag!("\n") >> b: multispace >> ())) // (vec![from_opt(c), a, b]))
recognize!(do_parse!(a: opt!(tag!("\n")) >> b: eof!() >> ())) |
recognize!(do_parse!(c: opt!(tag!(",")) >> a: tag!("\n") >> b: multispace >> ()))
)
);

Expand All @@ -186,12 +188,13 @@ named!(module_prolegomena<&str, ()>,
opt!(skip_comment) >>
step_indented >>
opt!(tag!(", ")) >>
opt!(tag!("- ")) >> // FIXME
(())
)
);

named_args!(module_helper<'a>(old: &'a str, new: &'a str)<&'a str, Vec<&'a str>>,
do_parse!(a: recognize!(module_prolegomena) >> b: is_not!("\r\n, ") >> c: alt!(tag!(",\n") | tag!(",") | line_ending ) >> (vec![a, swap_module(old, new, b), c]))
do_parse!(a: recognize!(module_prolegomena) >> b: is_not!("\r\n, ") >> c: alt!(tag!(",\n") | tag!(",") | line_ending) >> (vec![a, swap_module(old, new, b), c]))
);

named_args!(parse_modules<'a>(old: &'a str, new: &'a str)<&'a str, Vec<&'a str>>,
Expand Down
59 changes: 47 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,22 @@ fn module_to_file_names(module: &str, extension: &[String]) -> Vec<String> {
new_vec
}

fn replace_file(p: &PathBuf, old_module: &str, new_module: &str, _: &[String], _: bool) -> () {
fn replace_file(
p: &PathBuf,
old_module: &str,
new_module: &str,
source_contents: &str,
_: &[String],
_: bool,
) -> () {

let mut source_file = File::open(p).expect("174");
let mut source = String::new();
source_file.read_to_string(&mut source).expect("176");

let replacements = parse_haskell(
&source,
"Haskell",
source_contents,
&p.to_string_lossy().to_string(),
old_module,
new_module,
Expand All @@ -218,6 +225,7 @@ fn rayon_directory_contents(
old_module: &str,
new_module: &str,
extension: &[String],
source_contents: &str,
_: bool,
) -> () {

Expand All @@ -230,7 +238,7 @@ fn rayon_directory_contents(
source_file.read_to_string(&mut source).unwrap();
let replacements = parse_haskell(
&source,
"Haskell",
source_contents,
&p.to_string_lossy().to_string(),
old_module,
new_module,
Expand Down Expand Up @@ -272,7 +280,7 @@ fn read_file<P: AsRef<Path> + Debug>(p: P) -> String {
match file.read_to_string(&mut contents) {
Ok(_) => (),
_ => {
eprintln!("{}: Failed to read file at: {:?}", "Error".red(), contents);
eprintln!("{}: Failed to read file at: {:?}", "Error".red(), p);
exit(0x0001)
}
}
Expand Down Expand Up @@ -309,6 +317,7 @@ fn replace_all(
config: &ProjectOwned,
old_module: &str,
new_module: &str,
source_contents: &str,
benchmark_mode: bool,
) -> () {

Expand Down Expand Up @@ -393,7 +402,14 @@ fn replace_all(

// step 4: replace every 'import Module' with 'import NewModule'
if !config.copy {
rayon_directory_contents(config, old_module, new_module, module_ext, false);
rayon_directory_contents(
config,
old_module,
new_module,
module_ext,
source_contents,
false,
);
}

// step 5: move the actual file
Expand Down Expand Up @@ -427,7 +443,14 @@ fn replace_all(
.unwrap(),
);

replace_file(&file_name, old_module, new_module, module_ext, false);
replace_file(
&file_name,
old_module,
new_module,
source_contents,
module_ext,
false,
);

}

Expand Down Expand Up @@ -505,15 +528,26 @@ fn main() {
// TODO .hs-boot, .hsig files
let extns = vec![".hs".to_string(), ".x".to_string(), ".y".to_string()];

let config_project = get_config(&dir, &extns, ".cabal", command.is_present("copy"));
let config_extn = if command.is_present("hpack") {
"package.yaml"
} else {
".cabal"
};
let config_project = get_config(&dir, &extns, config_extn, command.is_present("copy"));

if command.is_present("stash") {
git_stash(&config_project.dir.to_string_lossy().to_string());
}

let benchmark = command.is_present("bench");

replace_all(&config_project, old_module, new_module, benchmark);
replace_all(
&config_project,
old_module,
new_module,
"Haskell",
benchmark,
);

if command.is_present("spec") {
let mut old_module_owned = old_module.to_string();
Expand All @@ -524,6 +558,7 @@ fn main() {
&config_project,
&old_module_owned,
&new_module_owned,
"Haskell",
benchmark,
);
}
Expand All @@ -546,7 +581,7 @@ fn main() {
git_stash(&config_project.dir.to_string_lossy().to_string());
}

replace_all(&config_project, old_config, new_config, false);
replace_all(&config_project, old_config, new_config, "Cabal", false);

} else if let Some(command) = matches.subcommand_matches("idris") {

Expand All @@ -566,7 +601,7 @@ fn main() {
git_stash(&config_project.dir.to_string_lossy().to_string());
}

replace_all(&config_project, old_module, new_module, false);
replace_all(&config_project, old_module, new_module, "Idris", false);

} else if let Some(command) = matches.subcommand_matches("elm") {

Expand All @@ -586,7 +621,7 @@ fn main() {
git_stash(&config_project.dir.to_string_lossy().to_string());
}

replace_all(&config_project, old_module, new_module, false);
replace_all(&config_project, old_module, new_module, "Elm", false);

} else if let Some(command) = matches.subcommand_matches("purescript") {

Expand All @@ -606,7 +641,7 @@ fn main() {
git_stash(&config_project.dir.to_string_lossy().to_string());
}

replace_all(&config_project, old_module, new_module, false);
replace_all(&config_project, old_module, new_module, "PureScript", false);

} else {
eprintln!("{}: failed to supply a subcommand", "Error".red());
Expand Down
8 changes: 4 additions & 4 deletions src/options-en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ subcommands:
index: 3
required: true
help: Name of the new module
# - hpack:
# short: y
# long: hpack
# help: "Flag to be used when the project uses an 'hpack.yaml' file rather than a '.cabal' file."
- hpack:
short: y
long: hpack
help: "Flag to be used when the project uses an 'hpack.yaml' file rather than a '.cabal' file."
- copy:
short: c
long: copy
Expand Down

0 comments on commit 94981c3

Please sign in to comment.