pub struct Makefile(/* private fields */);Expand description
An AST node for $ast
Implementations§
Source§impl Makefile
impl Makefile
Sourcepub fn read_relaxed<R: Read>(r: R) -> Result<Makefile, Error>
pub fn read_relaxed<R: Read>(r: R) -> Result<Makefile, Error>
Read makefile from a reader, but allow syntax errors
Sourcepub fn rules(&self) -> impl Iterator<Item = Rule> + '_
pub fn rules(&self) -> impl Iterator<Item = Rule> + '_
Retrieve the rules in the makefile
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "rule: dependency\n\tcommand\n".parse().unwrap();
assert_eq!(makefile.rules().count(), 1);Sourcepub fn rules_by_target<'a>(
&'a self,
target: &'a str,
) -> impl Iterator<Item = Rule> + 'a
pub fn rules_by_target<'a>( &'a self, target: &'a str, ) -> impl Iterator<Item = Rule> + 'a
Get all rules that have a specific target
Sourcepub fn variable_definitions(&self) -> impl Iterator<Item = VariableDefinition>
pub fn variable_definitions(&self) -> impl Iterator<Item = VariableDefinition>
Get all variable definitions in the makefile
Sourcepub fn find_variable<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = VariableDefinition> + 'a
pub fn find_variable<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = VariableDefinition> + 'a
Find all variables by name
Returns an iterator over all variable definitions with the given name. Makefiles can have multiple definitions of the same variable.
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "VAR1 = value1\nVAR2 = value2\nVAR1 = value3\n".parse().unwrap();
let vars: Vec<_> = makefile.find_variable("VAR1").collect();
assert_eq!(vars.len(), 2);
assert_eq!(vars[0].raw_value(), Some("value1".to_string()));
assert_eq!(vars[1].raw_value(), Some("value3".to_string()));Sourcepub fn add_rule(&mut self, target: &str) -> Rule
pub fn add_rule(&mut self, target: &str) -> Rule
Add a new rule to the makefile
§Example
use makefile_lossless::Makefile;
let mut makefile = Makefile::new();
makefile.add_rule("rule");
assert_eq!(makefile.to_string(), "rule:\n");Sourcepub fn replace_rule(
&mut self,
index: usize,
new_rule: Rule,
) -> Result<(), Error>
pub fn replace_rule( &mut self, index: usize, new_rule: Rule, ) -> Result<(), Error>
Replace rule at given index with a new rule
§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "rule1:\n\tcommand1\nrule2:\n\tcommand2\n".parse().unwrap();
let new_rule: makefile_lossless::Rule = "new_rule:\n\tnew_command\n".parse().unwrap();
makefile.replace_rule(0, new_rule).unwrap();
assert!(makefile.rules().any(|r| r.targets().any(|t| t == "new_rule")));Sourcepub fn remove_rule(&mut self, index: usize) -> Result<Rule, Error>
pub fn remove_rule(&mut self, index: usize) -> Result<Rule, Error>
Remove rule at given index
§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "rule1:\n\tcommand1\nrule2:\n\tcommand2\n".parse().unwrap();
let removed = makefile.remove_rule(0).unwrap();
assert_eq!(removed.targets().collect::<Vec<_>>(), vec!["rule1"]);
assert_eq!(makefile.rules().count(), 1);Sourcepub fn insert_rule(&mut self, index: usize, new_rule: Rule) -> Result<(), Error>
pub fn insert_rule(&mut self, index: usize, new_rule: Rule) -> Result<(), Error>
Insert rule at given position
§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "rule1:\n\tcommand1\nrule2:\n\tcommand2\n".parse().unwrap();
let new_rule: makefile_lossless::Rule = "inserted_rule:\n\tinserted_command\n".parse().unwrap();
makefile.insert_rule(1, new_rule).unwrap();
let targets: Vec<_> = makefile.rules().flat_map(|r| r.targets().collect::<Vec<_>>()).collect();
assert_eq!(targets, vec!["rule1", "inserted_rule", "rule2"]);Sourcepub fn includes(&self) -> impl Iterator<Item = Include>
pub fn includes(&self) -> impl Iterator<Item = Include>
Get all include directives in the makefile
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "include config.mk\n-include .env\n".parse().unwrap();
let includes = makefile.includes().collect::<Vec<_>>();
assert_eq!(includes.len(), 2);Sourcepub fn included_files(&self) -> impl Iterator<Item = String> + '_
pub fn included_files(&self) -> impl Iterator<Item = String> + '_
Get all included file paths
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "include config.mk\n-include .env\n".parse().unwrap();
let paths = makefile.included_files().collect::<Vec<_>>();
assert_eq!(paths, vec!["config.mk", ".env"]);Sourcepub fn find_rule_by_target(&self, target: &str) -> Option<Rule>
pub fn find_rule_by_target(&self, target: &str) -> Option<Rule>
Find the first rule with a specific target name
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "rule1:\n\tcommand1\nrule2:\n\tcommand2\n".parse().unwrap();
let rule = makefile.find_rule_by_target("rule2");
assert!(rule.is_some());
assert_eq!(rule.unwrap().targets().collect::<Vec<_>>(), vec!["rule2"]);Sourcepub fn find_rules_by_target<'a>(
&'a self,
target: &'a str,
) -> impl Iterator<Item = Rule> + 'a
pub fn find_rules_by_target<'a>( &'a self, target: &'a str, ) -> impl Iterator<Item = Rule> + 'a
Find all rules with a specific target name
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "rule1:\n\tcommand1\nrule1:\n\tcommand2\nrule2:\n\tcommand3\n".parse().unwrap();
let rules: Vec<_> = makefile.find_rules_by_target("rule1").collect();
assert_eq!(rules.len(), 2);Sourcepub fn add_phony_target(&mut self, target: &str) -> Result<(), Error>
pub fn add_phony_target(&mut self, target: &str) -> Result<(), Error>
Add a target to .PHONY (creates .PHONY rule if it doesn’t exist)
§Example
use makefile_lossless::Makefile;
let mut makefile = Makefile::new();
makefile.add_phony_target("clean").unwrap();
assert!(makefile.is_phony("clean"));Sourcepub fn remove_phony_target(&mut self, target: &str) -> Result<bool, Error>
pub fn remove_phony_target(&mut self, target: &str) -> Result<bool, Error>
Remove a target from .PHONY (removes .PHONY rule if it becomes empty)
Returns true if the target was found and removed, false if it wasn’t in .PHONY.
If there are multiple .PHONY rules, it removes the target from the first rule that contains it.
§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = ".PHONY: clean test\n".parse().unwrap();
assert!(makefile.remove_phony_target("clean").unwrap());
assert!(!makefile.is_phony("clean"));
assert!(makefile.is_phony("test"));Sourcepub fn is_phony(&self, target: &str) -> bool
pub fn is_phony(&self, target: &str) -> bool
Check if a target is marked as phony
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = ".PHONY: clean test\n".parse().unwrap();
assert!(makefile.is_phony("clean"));
assert!(makefile.is_phony("test"));
assert!(!makefile.is_phony("build"));Sourcepub fn phony_targets(&self) -> impl Iterator<Item = String> + '_
pub fn phony_targets(&self) -> impl Iterator<Item = String> + '_
Get all phony targets
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = ".PHONY: clean test build\n".parse().unwrap();
let phony_targets: Vec<_> = makefile.phony_targets().collect();
assert_eq!(phony_targets, vec!["clean", "test", "build"]);