Repository: casey/just
File: src/recipe.rs (Rust)

Code before:
    };

    let mut tempdir_builder = tempfile::Builder::new();
    tempdir_builder.prefix("just-");
    let tempdir = match &context.module.settings.tempdir {
      Some(tempdir) => tempdir_builder.tempdir_in(context.search.working_directory.join(tempdir)),
      None => {
        if let Some(runtime_dir) = dirs::runtime_dir() {
          let path = runtime_dir.join("just");
          fs::create_dir_all(&path).map_err(|io_error| Error::RuntimeDirIo {
            io_error,
            path: path.clone(),
          })?;
          tempdir_builder.tempdir_in(path)
        } else {
          tempdir_builder.tempdir()
        }
      }
    }
    .map_err(|error| Error::TempdirIo {
      recipe: self.name(),
      io_error: error,
    })?;
    let mut path = tempdir.path().to_path_buf();

    let extension = self
      .attributes
      .get(AttributeDiscriminant::Extension)
      .and_then(|attribute| {
        if let Attribute::Extension(extension) = attribute {
          Some(extension.cooked.as_str())
        } else {
          None
        }
      });

    path.push(executor.script_filename(self.name(), extension));

    let script = executor.script(self, &evaluated_lines);

    if config.verbosity.grandiloquent() {
      eprintln!("{}", config.color.doc().stderr().paint(&script));
    }

    fs::write(&path, script).map_err(|error| Error::TempdirIo {
      recipe: self.name(),
      io_error: error,
    })?;

    let mut command = executor.command(
      &path,

Review comment (refactor):
I think the original version of this is actually better, since we don't duplicate which attribute we're looking for.

Code after:
      )
    } else {
      let line = evaluated_lines
        .first()
        .ok_or_else(|| Error::internal("evaluated_lines was empty"))?;

      let shebang =
        Shebang::new(line).ok_or_else(|| Error::internal(format!("bad shebang line: {line}")))?;

      Executor::Shebang(shebang)
    };

    let mut tempdir_builder = tempfile::Builder::new();
    tempdir_builder.prefix("just-");
    let tempdir = match &context.module.settings.tempdir {
      Some(tempdir) => tempdir_builder.tempdir_in(context.search.working_directory.join(tempdir)),
      None => {
        if let Some(runtime_dir) = dirs::runtime_dir() {
          let path = runtime_dir.join("just");
          fs::create_dir_all(&path).map_err(|io_error| Error::RuntimeDirIo {
            io_error,
            path: path.clone(),
          })?;
          tempdir_builder.tempdir_in(path)
        } else {
          tempdir_builder.tempdir()
        }
      }
    }
    .map_err(|error| Error::TempdirIo {
      recipe: self.name(),
      io_error: error,
    })?;
    let mut path = tempdir.path().to_path_buf();

    let extension = self.attributes.iter().find_map(|attribute| {
      if let Attribute::Extension(extension) = attribute {
        Some(extension.cooked.as_str())
      } else {
        None
      }
    });

    path.push(executor.script_filename(self.name(), extension));

    let script = executor.script(self, &evaluated_lines);

    if config.verbosity.grandiloquent() {
      eprintln!("{}", config.color.doc().stderr().paint(&script));
    }