Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/node insertion #17

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
100 changes: 100 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ rowan = "0.12.6"
parse-display = "0.4.1"
anyhow = "1.0"
rust-nix-templater = { git = "https://github.com/yusdacra/rust-nix-templater.git", branch = "master" }
either = "1.6.1"
nixpkgs-fmt = "1.1.0"

[package.metadata.nix]
app = true
Expand Down
23 changes: 6 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,12 @@ their package and set of dependencies.

The idea is to have the user specify the type of dependencies,
flake inputs and outputs, and type of the package with `skim`,
then to output a flake. This flake is then validated with `rnix`.
then to output a flake. This flake is then validated with `rnix`
formatted with `nixpkgs-fmt`, then written to a file.

If the user wants to modify an existing flake to add or remove
dependencies, this will also be possible. The flake shall be
parsed in with `rnix`, and the user will be able to modify it.

As of now, basically none of the features exist. I've only
got the proof of concept working: skim can be used for a cli
and rnix can be used to modify the AST.

Further down the line, I'd like to make this even more interactive.
This will involve querying github, crates.io, pypy, nixpkgs and more for packages,
then piping them into skim for selection based on language.

The hope is to also provide automatic support for pre-existing
nix expression generators such as node2nix, poetry2nix, cabal2nix,
and naersk.
parsed with `rnix`, and the user will be able to modify it.

# Dependencies #

Expand All @@ -60,11 +49,11 @@ and the `skim` fuzzy finder for the cli.
# Roadmap #

- [x] Proof of concept
- [ ] Flake Input management
- [ ] Add inputs
- [x] Flake Input management
- [x] Add inputs
- [ ] Remove inputs
- [ ] Change inputs
- [ ] Query github
- [ ] Query github into skim (this is hard...may not be feasible..)
- [ ] BuildInput management
- [ ] Query nixpkgs
- [ ] Modify buildInputs
Expand Down
138 changes: 105 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
mod parser;
mod user;

use crate::parser::input_utils::SyntaxStructure;
use anyhow::anyhow;
use nixpkgs_fmt::reformat_node;
use parser::file::{filename_to_node, write_to_node};
use parser::utils::remove_input;
use parser::input_utils::{merge_attr_sets, Input};
use parser::utils::{get_node_idx, remove_input, search_for_attr, NixNode};
use rowan::GreenNode;
use rowan::NodeOrToken;
use rowan::SyntaxNode;
DieracDelta marked this conversation as resolved.
Show resolved Hide resolved
use user::*;

struct ActionStack {
Expand All @@ -21,6 +27,12 @@ impl ActionStack {
self.inner.push(action)
}

fn push_seq(&mut self, actions: impl IntoIterator<Item = UserAction>) {
actions
.into_iter()
.for_each(|action| self.inner.push(action))
DieracDelta marked this conversation as resolved.
Show resolved Hide resolved
}

fn pop(&mut self) -> UserAction {
self.inner.pop().unwrap_or(Self::START_ACTION)
}
Expand Down Expand Up @@ -64,49 +76,109 @@ fn main() {
UserPrompt::Create => action_stack.push(UserAction::CreateNew),
UserPrompt::Modify => action_stack.push(UserAction::ModifyExisting),
UserPrompt::DeleteInput => action_stack.push(UserAction::RemoveInput),
UserPrompt::AddInput => action_stack.push(UserAction::AddInput),
UserPrompt::AddInput => action_stack.push_seq(vec![
UserAction::IntroParsed,
UserAction::ConfirmWrite,
UserAction::ConfirmInputCorrect,
UserAction::QueryInputName,
UserAction::QueryInputUrl,
UserAction::IsInputFlake,
]),
UserPrompt::SelectLang(lang) => match lang {
Lang::Rust => action_stack.push(UserAction::Rust(user::rust::Action::Intro)),
lang => todo!("lang {}", lang),
},
UserPrompt::Rust(prompt) => {
prompt.process_prompt(&mut action_stack, &mut user_data);
}
UserPrompt::Other(other) => {
match cur_action {
UserAction::Rust(action) => {
action
.clone()
.process_action(other, &mut action_stack, &mut user_data)
}
UserAction::ModifyExisting => {
let filename = other.0.as_str();
match filename_to_node(filename, &other) {
Err(err_msg) => action_stack.push(UserAction::Error(err_msg)),
Ok(root) => {
user_data.filename = Some(filename.to_string());
user_data.root = Some(root);
action_stack.push(UserAction::IntroParsed);
}
UserPrompt::Other(other) => match cur_action {
UserAction::Rust(action) => {
action
.clone()
.process_action(other, &mut action_stack, &mut user_data)
}
UserAction::QueryInputUrl => {
let mut i = user_data.new_input.unwrap_or_default();
i.url = Some(SyntaxStructure::StringLiteral(other));
user_data.new_input = Some(i);
action_stack.pop();
}
UserAction::QueryInputName => {
let mut i = user_data.new_input.unwrap_or_default();
i.name = Some(SyntaxStructure::StringLiteral(other));
user_data.new_input = Some(i);
action_stack.pop();
}
UserAction::ModifyExisting => {
let filename = other.0.as_str();
match filename_to_node(filename, &other) {
Err(err_msg) => action_stack.push(UserAction::Error(err_msg)),
Ok(root) => {
user_data.filename = Some(filename.to_string());
user_data.root = Some(root);
action_stack.push(UserAction::IntroParsed);
}
}
UserAction::RemoveInput => {
let inputs = user_data.inputs.as_ref().unwrap();
let new_root = remove_input(
user_data.root.as_ref().unwrap(),
other.0.as_str(),
Some(inputs),
)
.unwrap();
user_data.new_root(new_root);
write_to_node(&user_data);

// TODO add in a "write to file" option at the end instead of writing after every modification
action_stack.push(UserAction::IntroParsed);
}
UserAction::RemoveInput => {
let inputs = user_data.inputs.as_ref().unwrap();
let new_root = remove_input(
user_data.root.as_ref().unwrap(),
other.0.as_str(),
Some(inputs),
)
.unwrap();
user_data.new_root(new_root);
action_stack.pop();
action_stack.push(UserAction::IntroParsed);
}
_ => unimplemented!(),
},
UserPrompt::Bool(b) => match cur_action {
UserAction::IsInputFlake => {
action_stack.pop();
let mut i = user_data
.new_input
.as_ref()
.and_then(|x: &Input| Some(x.clone()))
DieracDelta marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or_default();
i.is_flake = Some(SyntaxStructure::Bool(b));
user_data.new_input = Some(i);
}
UserAction::ConfirmInputCorrect => {
let root = user_data.root.as_ref().unwrap();
let (inputs, _, _) = search_for_attr("inputs", 1, root, None)[0].clone();
let new_input: GreenNode = user_data
.new_input
.as_ref()
.and_then(|x| Some(x.clone()))
DieracDelta marked this conversation as resolved.
Show resolved Hide resolved
.unwrap()
.into();
let augmented_input = merge_attr_sets(inputs.green().to_owned(), new_input);
let idx = get_node_idx(&inputs).unwrap();
let parent = inputs.parent().unwrap();
let new_root = inputs
.parent()
.unwrap()
.green()
.to_owned()
.replace_child(idx, NodeOrToken::Node(augmented_input));
let mut new_root_wrapped: NixNode =
SyntaxNode::new_root(parent.replace_with(new_root));
while let Some(parent) = new_root_wrapped.parent() {
new_root_wrapped = parent;
}
_ => unimplemented!(),
action_stack.pop();
user_data.new_root(reformat_node(&new_root_wrapped))
}
}
UserAction::ConfirmWrite => {
action_stack.pop();
if b {
write_to_node(&user_data)
}
}
_ => unimplemented!("bool not implemented in this case {}", cur_action),
},
}
}
}
Loading