cargo_config.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use anyhow::{Context, Result};
  2. use serde::Deserialize;
  3. use std::{
  4. fs,
  5. path::{Path, PathBuf},
  6. };
  7. struct PathAncestors<'a> {
  8. current: Option<&'a Path>,
  9. }
  10. impl<'a> PathAncestors<'a> {
  11. fn new(path: &'a Path) -> PathAncestors<'a> {
  12. PathAncestors {
  13. current: Some(path),
  14. }
  15. }
  16. }
  17. impl<'a> Iterator for PathAncestors<'a> {
  18. type Item = &'a Path;
  19. fn next(&mut self) -> Option<&'a Path> {
  20. if let Some(path) = self.current {
  21. self.current = path.parent();
  22. Some(path)
  23. } else {
  24. None
  25. }
  26. }
  27. }
  28. #[derive(Default, Deserialize)]
  29. pub struct BuildConfig {
  30. target: Option<String>,
  31. }
  32. #[derive(Deserialize)]
  33. pub struct ConfigSchema {
  34. build: Option<BuildConfig>,
  35. }
  36. #[derive(Default)]
  37. pub struct Config {
  38. build: BuildConfig,
  39. }
  40. impl Config {
  41. pub fn load(path: &Path) -> Result<Self> {
  42. let mut config = Self::default();
  43. let get_config = |path: PathBuf| -> Result<ConfigSchema> {
  44. let contents = fs::read_to_string(&path)
  45. .with_context(|| format!("failed to read configuration file `{}`", path.display()))?;
  46. toml::from_str(&contents)
  47. .with_context(|| format!("could not parse TOML configuration in `{}`", path.display()))
  48. };
  49. for current in PathAncestors::new(path) {
  50. if let Some(path) = get_file_path(&current.join(".cargo"), "config", true)? {
  51. let toml = get_config(path)?;
  52. if let Some(target) = toml.build.and_then(|b| b.target) {
  53. config.build.target = Some(target);
  54. break;
  55. }
  56. }
  57. }
  58. if config.build.target.is_none() {
  59. if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
  60. if let Some(path) = get_file_path(&PathBuf::from(cargo_home), "config", true)? {
  61. let toml = get_config(path)?;
  62. if let Some(target) = toml.build.and_then(|b| b.target) {
  63. config.build.target = Some(target);
  64. }
  65. }
  66. }
  67. }
  68. Ok(config)
  69. }
  70. pub fn build(&self) -> &BuildConfig {
  71. &self.build
  72. }
  73. }
  74. impl BuildConfig {
  75. pub fn target(&self) -> Option<&str> {
  76. self.target.as_deref()
  77. }
  78. }
  79. /// The purpose of this function is to aid in the transition to using
  80. /// .toml extensions on Cargo's config files, which were historically not used.
  81. /// Both 'config.toml' and 'credentials.toml' should be valid with or without extension.
  82. /// When both exist, we want to prefer the one without an extension for
  83. /// backwards compatibility, but warn the user appropriately.
  84. fn get_file_path(
  85. dir: &Path,
  86. filename_without_extension: &str,
  87. warn: bool,
  88. ) -> Result<Option<PathBuf>> {
  89. let possible = dir.join(filename_without_extension);
  90. let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension));
  91. if possible.exists() {
  92. if warn && possible_with_extension.exists() {
  93. // We don't want to print a warning if the version
  94. // without the extension is just a symlink to the version
  95. // WITH an extension, which people may want to do to
  96. // support multiple Cargo versions at once and not
  97. // get a warning.
  98. let skip_warning = if let Ok(target_path) = fs::read_link(&possible) {
  99. target_path == possible_with_extension
  100. } else {
  101. false
  102. };
  103. if !skip_warning {
  104. log::warn!(
  105. "Both `{}` and `{}` exist. Using `{}`",
  106. possible.display(),
  107. possible_with_extension.display(),
  108. possible.display()
  109. );
  110. }
  111. }
  112. Ok(Some(possible))
  113. } else if possible_with_extension.exists() {
  114. Ok(Some(possible_with_extension))
  115. } else {
  116. Ok(None)
  117. }
  118. }