-
Notifications
You must be signed in to change notification settings - Fork 71
Fix #1180 Add the ability to build filters completely locally #1181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alex-302
wants to merge
18
commits into
master
Choose a base branch
from
fix/1180_local_build_from_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a8ac982
Fix #1180 Add the ability to build filters completely locally
Alex-302 dce899d
fix/lint errors in build.js
Alex-302 395bd2f
fix/Add --strip-generated-meta build flag for debug purposes
Alex-302 b656361
fix/lint
Alex-302 a1af0ab
merge parent branch into current one, resolve conflicts
slvvko 53c6a5c
- strip_generated_meta.js renamed to kebab-case
Alex-302 7af7209
Reused find_files.js in strip-generated-meta.js and build.js
Alex-302 576f7ef
use fs/promises in strip-generated-meta
Alex-302 a12947d
convert strip-generated-meta.js to TS
Alex-302 54a6df8
Upd docs
Alex-302 7a10cdb
Fixed invoking strip-generated-meta
Alex-302 337d24a
Fix lint error
Alex-302 9c694a6
strip meta in both platforms/ and temp/platforms/, add tests
Alex-302 6875b74
lint errors
Alex-302 a6919b5
Fix recursive walk inside platforms dirs
Alex-302 aea8ac6
feat(build): add CLI argument validation, added tests
Alex-302 a927ae8
Added eol-last in build-config.ts
Alex-302 3c74498
fix md
Alex-302 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,16 +8,22 @@ import { | |
| FOLDER_WITH_NEW_FILTERS, | ||
| FOLDER_WITH_OLD_FILTERS, | ||
| } from './constants.js'; | ||
| import { stripGeneratedMetaFromDir } from './strip_generated_meta.js'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| /** | ||
| * Parse command-cli parameters -i|--include and -s|--skip | ||
| * Parse command-line parameters -i|--include, -s|--skip, --use-cache, --generate-cache, | ||
| * --no-patches-prepare, --strip-generated-meta | ||
| */ | ||
| let includedFilterIDs = []; | ||
| let excludedFilterIDs = []; | ||
| let rawReportPath = ''; | ||
| let useCache = false; | ||
| let generateCache = false; | ||
| let noPatchesPrepare = false; | ||
| let stripGeneratedMeta = false; | ||
|
|
||
| const args = process.argv.slice(2); | ||
| args.forEach((val) => { | ||
|
|
@@ -40,26 +46,128 @@ args.forEach((val) => { | |
| if (val.startsWith('--report=')) { | ||
| rawReportPath = val.slice(val.indexOf('=') + 1).trim(); | ||
| } | ||
|
|
||
| if (val === '--use-cache') { | ||
| useCache = true; | ||
| } | ||
|
|
||
| if (val === '--generate-cache') { | ||
|
Alex-302 marked this conversation as resolved.
Outdated
|
||
| generateCache = true; | ||
| } | ||
|
|
||
| if (val === '--no-patches-prepare') { | ||
| noPatchesPrepare = true; | ||
| } | ||
|
|
||
| if (val === '--strip-generated-meta') { | ||
| stripGeneratedMeta = true; | ||
| } | ||
| }); | ||
|
|
||
| if (useCache && generateCache) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error: --use-cache and --generate-cache are mutually exclusive.'); | ||
| process.exit(1); | ||
| } | ||
|
Comment on lines
+53
to
+57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: this block is dead code now — |
||
|
|
||
| /** | ||
| * Set all relative paths needed for compiler | ||
| */ | ||
| const filtersDir = path.join(__dirname, '../../filters'); | ||
| const logPath = path.join(__dirname, '../../log.txt'); | ||
| const platformsPath = path.join(__dirname, '../..', FOLDER_WITH_NEW_FILTERS); | ||
| const copyPlatformsPath = path.join(__dirname, '../..', FOLDER_WITH_OLD_FILTERS); | ||
| const cachedFiltersDir = path.join(__dirname, '../../temp/filters_cached'); | ||
|
|
||
| const reportPath = rawReportPath !== '' | ||
| // report-adguard.txt OR report-third-party.txt | ||
| ? path.join(__dirname, '../..', rawReportPath) | ||
| // report_partial_DD-MM-YYYY_HH-MM-SS.txt | ||
| : path.join(__dirname, '../..', `report_partial_${formatDate(new Date())}.txt`); | ||
|
|
||
| const SHADOW_TEMPLATE_CONTENT = '@include "./filter.txt"\n'; | ||
|
|
||
| /** | ||
| * Recursively find all `template.txt` files under the given directory. | ||
| * | ||
| * @param {string} dir - Root directory to search. | ||
| * @returns {Promise<string[]>} Array of absolute paths to `template.txt` files. | ||
| */ | ||
| const findTemplatePaths = async (dir) => { | ||
|
Alex-302 marked this conversation as resolved.
Outdated
|
||
| const entries = await fs.promises.readdir(dir, { withFileTypes: true }); | ||
|
|
||
| const results = await Promise.all(entries.map(async (entry) => { | ||
| const fullPath = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| return findTemplatePaths(fullPath); | ||
| } | ||
| if (entry.name === 'template.txt') { | ||
| return [fullPath]; | ||
| } | ||
| return []; | ||
| })); | ||
|
|
||
| return results.flat(); | ||
| }; | ||
|
|
||
| /** | ||
| * Prepare a temporary copy of the filters directory with shadow templates. | ||
| * | ||
| * Copies `filters/` → `temp/filters_cached/`, then replaces the content of every | ||
| * `template.txt` with a single-line local include pointing to the cached `filter.txt`. | ||
| * Validates that every filter directory with a `template.txt` also has a `filter.txt`. | ||
| * | ||
| * @returns {Promise<void>} | ||
| */ | ||
| const prepareCachedFiltersDir = async () => { | ||
| // Remove stale copy if exists | ||
| if (fs.existsSync(cachedFiltersDir)) { | ||
| await fs.promises.rm(cachedFiltersDir, { recursive: true }); | ||
| } | ||
|
|
||
| // Full recursive copy | ||
| await fs.promises.cp(filtersDir, cachedFiltersDir, { recursive: true }); | ||
|
|
||
| // Find all directories containing template.txt and replace with shadow templates | ||
| const templatePaths = await findTemplatePaths(cachedFiltersDir); | ||
|
|
||
| await Promise.all(templatePaths.map(async (templatePath) => { | ||
| const dir = path.dirname(templatePath); | ||
| const filterTxtPath = path.join(dir, 'filter.txt'); | ||
|
|
||
| if (!fs.existsSync(filterTxtPath)) { | ||
| throw new Error( | ||
| `--use-cache: missing filter.txt in ${path.relative(cachedFiltersDir, dir)}. ` | ||
| + 'Run "yarn generate-cache" first to generate cached filter files.', | ||
| ); | ||
| } | ||
|
|
||
| await fs.promises.writeFile(templatePath, SHADOW_TEMPLATE_CONTENT, 'utf8'); | ||
| })); | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log(`Prepared cached filters directory with ${templatePaths.length} shadow templates.`); | ||
| }; | ||
|
|
||
| /** | ||
| * Compiler entry point. | ||
| */ | ||
| const buildFilters = async () => { | ||
|
Alex-302 marked this conversation as resolved.
|
||
| // When --generate-cache we only need to compile filters (which updates filter.txt), | ||
| // skip platform generation, patches preparation, and temp/platforms copying. | ||
| if (generateCache) { | ||
| await compile( | ||
| filtersDir, | ||
| logPath, | ||
| reportPath, | ||
| null, // null ⇒ generate() inside compiler returns early, no platform files | ||
| includedFilterIDs, | ||
| excludedFilterIDs, | ||
| CUSTOM_PLATFORMS_CONFIG, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Clean temporary folder | ||
| if (fs.existsSync(copyPlatformsPath)) { | ||
| await fs.promises.rm(copyPlatformsPath, { recursive: true }); | ||
|
|
@@ -70,27 +178,49 @@ const buildFilters = async () => { | |
| let initialRun = false; | ||
| if (!fs.existsSync(platformsPath)) { | ||
| initialRun = true; | ||
| } else { | ||
| } else if (!noPatchesPrepare) { | ||
| // Make copy for future patches generation | ||
| await fs.promises.cp(platformsPath, copyPlatformsPath, { recursive: true }); | ||
| } | ||
|
|
||
| await compile( | ||
| filtersDir, | ||
| logPath, | ||
| reportPath, | ||
| platformsPath, | ||
| includedFilterIDs, | ||
| excludedFilterIDs, | ||
| CUSTOM_PLATFORMS_CONFIG, | ||
| ); | ||
| // Determine which filtersDir to pass to the compiler | ||
| const effectiveFiltersDir = useCache ? cachedFiltersDir : filtersDir; | ||
|
|
||
| if (useCache) { | ||
| await prepareCachedFiltersDir(); | ||
| } | ||
|
|
||
| try { | ||
| await compile( | ||
| effectiveFiltersDir, | ||
| logPath, | ||
| reportPath, | ||
| platformsPath, | ||
| includedFilterIDs, | ||
| excludedFilterIDs, | ||
| CUSTOM_PLATFORMS_CONFIG, | ||
| ); | ||
| } finally { | ||
| // Clean up temp filters copy | ||
| if (useCache && fs.existsSync(cachedFiltersDir)) { | ||
| await fs.promises.rm(cachedFiltersDir, { recursive: true }); | ||
| } | ||
| } | ||
|
|
||
| // For the very first run, we should copy the built platforms into | ||
| // the temp folder to create the first empty patches for future versions | ||
| if (initialRun) { | ||
| if (initialRun && !noPatchesPrepare) { | ||
| // Make copy for future patches generation | ||
| await fs.promises.cp(platformsPath, copyPlatformsPath, { recursive: true }); | ||
| } | ||
|
|
||
| // Strip generated metadata (Checksum, Diff-Path, TimeUpdated, Version) | ||
| // from compiled filter files so they don't pollute diff comparisons. | ||
| if (stripGeneratedMeta) { | ||
|
Alex-302 marked this conversation as resolved.
|
||
| const count = await stripGeneratedMetaFromDir(platformsPath); | ||
| // eslint-disable-next-line no-console | ||
| console.log(`Stripped generated meta from ${count} file(s).`); | ||
| } | ||
| }; | ||
|
|
||
| buildFilters(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.