All files / PARSE/1-Discover PlatformIOIni.ts

99% Statements 100/101
94.59% Branches 70/74
100% Functions 21/21
100% Lines 94/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344                              52x     52x     52x             52x                                       125x   125x   446x 125x 125x 125x 295x 295x 295x 295x 149x 149x   146x 146x 1x   145x 145x       12x 12x   145x 102x   145x   125x         63x 63x 34x 34x 12x     63x                       122x 122x 93x   29x               61x 61x 61x 67x 25x   42x   42x   67x     67x           61x     61x                                           116x 61x 8x         8x         61x   14x                             61x         61x 122x   61x 61x 8x   61x                                           87x 87x                   156x 156x 156x 199x 199x 195x 195x 199x 139x   115x   76x                             84x 5x 5x 5x 5x             5x 5x   5x                           5x 4x 1x       3x 5x                 42x 42x                       196x   218x 218x                 157x       79x                  
/**
 * The one reader of platformio.ini.
 *
 * Include discovery reads `lib_extra_dirs` from it, and the run's target
 * (ADR-049, the build-system rung) reads each environment's board and
 * platform. Both go through `sections()`, so the file's continuation and
 * comment rules are decided once.
 */
import { join } from "node:path";
 
import type IFileSystem from "../../types/IFileSystem";
import type IPlatformIOEnv from "../../types/IPlatformIOEnv";
import type IPlatformIOProject from "../../types/IPlatformIOProject";
 
/** How deep `${section.option}` may nest before a value is taken as cyclic */
const MAX_INTERPOLATION = 8;
 
/** A `${section.option}` reference, as PlatformIO spells one */
const REFERENCE = /\$\{([^.}]+)\.([^}]+)\}/g;
 
/** `default_envs`, and the old name PlatformIO still reads it by */
const DEFAULT_ENVS: readonly string[] = ["default_envs", "env_default"];
 
/**
 * The build machine's variables that add to `default_envs`: the current
 * name, then the old one PlatformIO reads when it is unset. An empty value
 * counts as unset, as PlatformIO's own option reader counts it.
 */
const MACHINE_DEFAULT_ENVS: readonly string[] = [
  "PLATFORMIO_DEFAULT_ENVS",
  "PLATFORMIO_ENV_DEFAULT",
];
 
/** The build machine's environment variables, as `process.env` holds them */
type TEnvironment = Readonly<Record<string, string | undefined>>;
 
class PlatformIOIni {
  /**
   * Section name -> key -> value, in file order, read as PlatformIO's
   * configparser reads the file. Blank lines and comment lines are skipped,
   * so neither ends a value (#1760 review: both did), and a comment after
   * whitespace is dropped from its line. A value continues onto each
   * following line that is indented, does not start a key of its own, and
   * does not open a section (#1181). Each line is trimmed and the lines are
   * joined with "\n", so a value written below its key starts with one. Keys
   * before any section belong to the section named "".
   */
  static sections(content: string): Map<string, Map<string, string>> {
    const sections = new Map<string, Map<string, string>>();
    // #1760 review: a CRLF file kept `\r` on every line, so no key matched
    const lines = content
      .split(/\r?\n/)
      .filter((line) => !/^\s*(?:[;#]|$)/.test(line));
    let section = "";
    let index = 0;
    while (index < lines.length) {
      const line = lines[index];
      index += 1;
      const header = /^\s*\[([^\]]*)\]/.exec(line);
      if (header) {
        section = header[1].trim();
        continue;
      }
      const key = /^[ \t]*([\w.]+)[ \t]*=(.*)$/.exec(line);
      if (!key) {
        continue;
      }
      const collected = [PlatformIOIni.uncomment(key[2])];
      while (
        index < lines.length &&
        PlatformIOIni.isContinuation(lines[index])
      ) {
        collected.push(PlatformIOIni.uncomment(lines[index]));
        index += 1;
      }
      if (!sections.has(section)) {
        sections.set(section, new Map());
      }
      sections.get(section)!.set(key[1], collected.join("\n"));
    }
    return sections;
  }
 
  /** Every section's value for `key`, in file order */
  static valuesOf(content: string, key: string): string[] {
    const values: string[] = [];
    for (const keys of PlatformIOIni.sections(content).values()) {
      const value = keys.get(key);
      if (value !== undefined) {
        values.push(value);
      }
    }
    return values;
  }
 
  /**
   * The platformio.ini in `projectRoot`, or null when there is none, read with
   * the build machine's `environment` as PlatformIO reads it
   */
  static read(
    projectRoot: string,
    fs: IFileSystem,
    environment: TEnvironment,
  ): IPlatformIOProject | null {
    const path = join(projectRoot, "platformio.ini");
    if (!fs.exists(path)) {
      return null;
    }
    return PlatformIOIni.project(path, fs.readFile(path), environment);
  }
 
  static project(
    path: string,
    content: string,
    environment: TEnvironment,
  ): IPlatformIOProject {
    const sections = PlatformIOIni.sections(content);
    const envs: IPlatformIOEnv[] = [];
    for (const name of sections.keys()) {
      if (!name.startsWith("env:")) {
        continue;
      }
      const envName = name.slice("env:".length).trim();
      const board =
        PlatformIOIni.optionValue(sections, name, ["board"])?.trim() ||
        undefined;
      const platform = PlatformIOIni.platformName(
        PlatformIOIni.optionValue(sections, name, ["platform"]),
      );
      envs.push({
        name: envName,
        ...(board === undefined ? {} : { board }),
        ...(platform === undefined ? {} : { platform }),
      });
    }
    const defaultEnvs = PlatformIOIni.list(
      PlatformIOIni.defaultEnvsValue(sections, environment) ?? "",
    );
    return {
      path,
      envs,
      defaultEnvs,
      machineDefaultEnvs: PlatformIOIni.machineDefaultEnvs(
        sections,
        environment,
        defaultEnvs,
      ),
    };
  }
 
  /**
   * Which of `defaultEnvs` the build machine's variable added: the names the
   * file's own `default_envs` does not list. The list itself is split once,
   * from the joined value, as PlatformIO splits it; this only attributes it.
   */
  private static machineDefaultEnvs(
    sections: ReadonlyMap<string, ReadonlyMap<string, string>>,
    environment: TEnvironment,
    defaultEnvs: readonly string[],
  ): IPlatformIOProject["machineDefaultEnvs"] {
    const variable = MACHINE_DEFAULT_ENVS.find((name) => environment[name]);
    if (variable === undefined) return null;
    const fromFile = PlatformIOIni.walkValue(
      sections,
      "platformio",
      DEFAULT_ENVS,
    );
    const fileNames = PlatformIOIni.list(
      fromFile === undefined
        ? ""
        : (PlatformIOIni.expand(fromFile, sections, "platformio", 0) ?? ""),
    );
    return {
      variable,
      names: defaultEnvs.filter((name) => !fileNames.includes(name)),
    };
  }
 
  /**
   * `default_envs` as PlatformIO reads it: the file's value, with the build
   * machine's `PLATFORMIO_DEFAULT_ENVS` appended on a line of its own, or its
   * old name `PLATFORMIO_ENV_DEFAULT` when that is unset. References expand
   * after the append, as PlatformIO's do. #1794, owner ruling 2026-09-28:
   * "yes, if it exists".
   */
  private static defaultEnvsValue(
    sections: ReadonlyMap<string, ReadonlyMap<string, string>>,
    environment: TEnvironment,
  ): string | undefined {
    const fromFile = PlatformIOIni.walkValue(
      sections,
      "platformio",
      DEFAULT_ENVS,
    );
    const fromMachine = MACHINE_DEFAULT_ENVS.map(
      (name) => environment[name],
    ).find(Boolean);
    let raw = fromFile;
    if (fromMachine) {
      raw = fromFile ? `${fromFile}\n${fromMachine}` : fromMachine;
    }
    return raw === undefined
      ? undefined
      : PlatformIOIni.expand(raw, sections, "platformio", 0);
  }
 
  /**
   * A section's value for an option known by any of `names`, found as
   * PlatformIO finds it (its config's `walk_options`), with its
   * `${section.option}` references expanded: the section's own options, then
   * the sections it `extends` -- the LAST listed first, each one's own parents
   * before the next -- and for an env the common `[env]` section last. The
   * first section giving the option answers, even with an empty value. A
   * section is visited once, so a cyclic `extends` ends. #1760 review: `[env]`
   * answered for the first parent before the rest were read, so
   * `extends = flags, teensy_base` built `[env]`'s board.
   */
  private static optionValue(
    sections: ReadonlyMap<string, ReadonlyMap<string, string>>,
    section: string,
    names: readonly string[],
    depth = 0,
  ): string | undefined {
    const raw = PlatformIOIni.walkValue(sections, section, names);
    return raw === undefined
      ? undefined
      : PlatformIOIni.expand(raw, sections, section, depth);
  }
 
  private static walkValue(
    sections: ReadonlyMap<string, ReadonlyMap<string, string>>,
    section: string,
    names: readonly string[],
  ): string | undefined {
    const stack = section.startsWith("env:") ? ["env", section] : [section];
    const visited = new Set<string>();
    while (stack.length > 0) {
      const name = stack.pop()!;
      if (visited.has(name)) continue;
      visited.add(name);
      const options = sections.get(name) ?? new Map<string, string>();
      for (const [option, value] of options) {
        if (names.includes(option)) return value;
      }
      stack.push(...PlatformIOIni.list(options.get("extends") ?? ""));
    }
    return undefined;
  }
 
  /**
   * A value with its `${section.option}` references expanded, as PlatformIO
   * expands them. Undefined when a reference cannot be expanded here -- the
   * build machine's `${sysenv.X}`, a missing option, a cycle -- so the value
   * says nothing. #1760 review: `board = ${common.board}` gave a false E0510.
   */
  private static expand(
    value: string,
    sections: ReadonlyMap<string, ReadonlyMap<string, string>>,
    section: string,
    depth: number,
  ): string | undefined {
    if (!value.includes("${")) return value;
    Iif (depth >= MAX_INTERPOLATION) return undefined;
    let unexpanded = false;
    const expanded = value.replace(REFERENCE, (_whole, from, option) => {
      const found = PlatformIOIni.reference(
        sections,
        section,
        String(from),
        String(option),
        depth,
      );
      unexpanded ||= found === undefined;
      return found ?? "";
    });
    return unexpanded || expanded.includes("${") ? undefined : expanded;
  }
 
  /**
   * One `${from.option}`: `this` is the section asked about, whose env name
   * is `${this.__env__}`, and `sysenv` is the build machine's, so unknown here
   */
  private static reference(
    sections: ReadonlyMap<string, ReadonlyMap<string, string>>,
    section: string,
    from: string,
    option: string,
    depth: number,
  ): string | undefined {
    if (from === "sysenv") return undefined;
    if (from === "this" && option === "__env__") {
      return section.startsWith("env:")
        ? section.slice("env:".length)
        : undefined;
    }
    const named = from === "this" ? section : from;
    return PlatformIOIni.optionValue(sections, named, [option], depth + 1);
  }
 
  /**
   * A platform's name from its PlatformIO spec: `atmelavr` from
   * `atmelavr@~4.2.0` and from `platformio/atmelavr` (#1760 review: both
   * gave a false E0510)
   */
  private static platformName(spec: string | undefined): string | undefined {
    const name = spec?.split("@")[0].split("/").at(-1)?.trim();
    return name || undefined;
  }
 
  /**
   * A value's items as PlatformIO splits one (`parse_multi_values`): by line
   * when it has several, else at ", " -- so `a,b` is ONE item -- each
   * trimmed, empties dropped. `sections()` has dropped the comments, line by
   * line, so a comma in a comment adds no item. The one list rule `extends`,
   * `default_envs` and `lib_extra_dirs` read. #1760 review: this split at
   * every comma, then cut comments from each piece.
   */
  static list(value: string): string[] {
    return value
      .split(value.includes("\n") ? "\n" : ", ")
      .map((item) => item.trim())
      .filter((item) => item.length > 0);
  }
 
  /**
   * A line's text without its comment, trimmed. A comment is `;` or `#`
   * after whitespace, as configparser has it (#1760 review:
   * `board = uno  # comment` kept the comment)
   */
  private static uncomment(text: string): string {
    return text.replace(/\s[;#].*$/, "").trim();
  }
 
  private static isContinuation(line: string): boolean {
    return (
      /^[ \t]+\S/.test(line) &&
      !/^[ \t]*[\w.]+[ \t]*=/.test(line) &&
      !line.trimStart().startsWith("[")
    );
  }
}
 
export default PlatformIOIni;