-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve `cloc` to use the much faster SCC program # Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels] * Remove stray string from iterating [ci skip-rust] [ci skip-build-wheels] * Review feedback [ci skip-rust] [ci skip-build-wheels] * Move `--args` to `scc` scope # Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
- Loading branch information
1 parent
c942604
commit fdac7c3
Showing
4 changed files
with
126 additions
and
175 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from typing import Tuple | ||
|
||
from pants.core.util_rules.external_tool import ( | ||
DownloadedExternalTool, | ||
ExternalTool, | ||
ExternalToolRequest, | ||
) | ||
from pants.engine.console import Console | ||
from pants.engine.fs import Digest, MergeDigests, SourcesSnapshot | ||
from pants.engine.goal import Goal, GoalSubsystem | ||
from pants.engine.platform import Platform | ||
from pants.engine.process import Process, ProcessResult | ||
from pants.engine.rules import Get, collect_rules, goal_rule | ||
from pants.option.custom_types import shell_str | ||
from pants.util.enums import match | ||
from pants.util.logging import LogLevel | ||
from pants.util.strutil import pluralize | ||
|
||
|
||
class SuccinctCodeCounter(ExternalTool): | ||
"""The Succinct Code Counter, aka `scc` (https://github.com/boyter/scc).""" | ||
|
||
options_scope = "scc" | ||
default_version = "2.12.0" | ||
default_known_versions = [ | ||
"2.12.0|darwin|70b7002cd1e4541cb37b7b9cbc0eeedd13ceacb49628e82ab46332bb2e65a5a6|1842530", | ||
"2.12.0|linux|8eca3e98fe8a78d417d3779a51724515ac4459760d3ec256295f80954a0da044|1753059", | ||
] | ||
|
||
@classmethod | ||
def register_options(cls, register) -> None: | ||
super().register_options(register) | ||
register( | ||
"--args", | ||
type=list, | ||
member_type=shell_str, | ||
passthrough=True, | ||
help=( | ||
'Arguments to pass directly to SCC, e.g. `--count-loc-args="--no-cocomo"`. Refer ' | ||
"to https://github.com/boyter/scc." | ||
), | ||
) | ||
|
||
@property | ||
def args(self) -> Tuple[str, ...]: | ||
return tuple(self.options.args) | ||
|
||
def generate_url(self, plat: Platform) -> str: | ||
plat_str = match(plat, {Platform.darwin: "apple-darwin", Platform.linux: "unknown-linux"}) | ||
return ( | ||
f"https://github.com/boyter/scc/releases/download/v{self.version}/scc-{self.version}-" | ||
f"x86_64-{plat_str}.zip" | ||
) | ||
|
||
def generate_exe(self, _: Platform) -> str: | ||
return "./scc" | ||
|
||
|
||
class CountLinesOfCodeSubsystem(GoalSubsystem): | ||
"""Count lines of code using `scc` (Succinct Code Counter, https://github.com/boyter/scc).""" | ||
|
||
name = "count-loc" | ||
|
||
|
||
class CountLinesOfCode(Goal): | ||
subsystem_cls = CountLinesOfCodeSubsystem | ||
|
||
|
||
@goal_rule | ||
async def count_loc( | ||
console: Console, | ||
succinct_code_counter: SuccinctCodeCounter, | ||
sources_snapshot: SourcesSnapshot, | ||
) -> CountLinesOfCode: | ||
if not sources_snapshot.snapshot.files: | ||
return CountLinesOfCode(exit_code=0) | ||
|
||
scc_program = await Get( | ||
DownloadedExternalTool, | ||
ExternalToolRequest, | ||
succinct_code_counter.get_request(Platform.current), | ||
) | ||
input_digest = await Get( | ||
Digest, MergeDigests((scc_program.digest, sources_snapshot.snapshot.digest)) | ||
) | ||
result = await Get( | ||
ProcessResult, | ||
Process( | ||
argv=(scc_program.exe, *succinct_code_counter.args), | ||
input_digest=input_digest, | ||
description=( | ||
f"Count lines of code for {pluralize(len(sources_snapshot.snapshot.files), 'file')}" | ||
), | ||
level=LogLevel.DEBUG, | ||
), | ||
) | ||
console.print_stdout(result.stdout.decode()) | ||
return CountLinesOfCode(exit_code=0) | ||
|
||
|
||
def rules(): | ||
return collect_rules() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters