11"""Runtime dependencies fetched from npm"""
22
33load ("@bazel_tools//tools/build_defs/repo:utils.bzl" , "maybe" )
4- load ("//ts/private:versions.bzl" , "TOOL_VERSIONS" )
4+ load ("//ts/private:versions.bzl" , "RULES_TS_VERSION" , " TOOL_VERSIONS" )
55
66worker_versions = struct (
77 bazel_worker_version = "5.4.2" ,
@@ -10,6 +10,17 @@ worker_versions = struct(
1010 google_protobuf_integrity = "sha512-XMf1+O32FjYIV3CYu6Tuh5PNbfNEU5Xu22X+Xkdb/DUexFlCzhvv7d5Iirm4AOwn8lv4al1YvIhzGrg2j9Zfzw==" ,
1111)
1212
13+ # Keep this list in sync with user documentation.
14+ # We must inform users what we gather from their builds.
15+ _TELEMETRY_VARS = [
16+ "BUILDKITE_BUILD_NUMBER" ,
17+ "BUILDKITE_ORGANIZATION_SLUG" ,
18+ "CIRCLE_BUILD_NUM" ,
19+ "CIRCLE_PROJECT_USERNAME" ,
20+ "GITHUB_REPOSITORY_OWNER" ,
21+ "GITHUB_RUN_NUMBER" ,
22+ ]
23+
1324def _http_archive_version_impl (rctx ):
1425 integrity = None
1526 if rctx .attr .version :
@@ -63,10 +74,54 @@ def _http_archive_version_impl(rctx):
6374 executable = False ,
6475 )
6576
77+ if rctx .attr .check_for_updates :
78+ _check_for_updates (rctx )
79+
80+ def _check_for_updates (rctx ):
81+ version = RULES_TS_VERSION
82+
83+ # If the placeholder string wasn't replaced, that means we aren't running from a release artifact.
84+ # It might be a SHA from GitHub, or a fork of the repo, etc.
85+ # We won't be able to say if an update is available, but we count these uses.
86+ if version .startswith ("$Format" ):
87+ version = "v0.0.0"
88+
89+ vars = ["{}={}" .format (v , rctx .os .environ [v ]) for v in _TELEMETRY_VARS if v in rctx .os .environ ]
90+ if rctx .attr .bzlmod :
91+ vars .append ("bzlmod=true" )
92+ url = "https://update.aspect.build/aspect_rules_ts/{}?{}" .format (
93+ version ,
94+ "&" .join (vars ),
95+ )
96+ output_path = str (rctx .path (".output/update_check_result" ))
97+ command = ["curl" , url , "--write-out" , "%{http_code}" , "--output" , output_path ]
98+
99+ # Avoid stalling the users Bazel session
100+ command .extend (["--connect-timeout" , "1" , "--max-time" , "1" ])
101+ result = rctx .execute (command )
102+ if result .return_code != 0 :
103+ # Ignore failures when trying to check for new version
104+ return
105+ status_code = int (result .stdout .strip ())
106+
107+ # 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.
108+ # Don't bother the user with any other status code
109+ if status_code != 302 :
110+ return
111+
112+ # buildifier: disable=print
113+ # TODO: print content of output_path which now has the link to the newer version
114+ print ("""\
115+ NOTICE: a newer version of rules_ts is available.
116+ See https://github.com/aspect-build/rules_ts/releases
117+ """ )
118+
66119http_archive_version = repository_rule (
67120 doc = "Re-implementation of http_archive that can read the version from package.json" ,
68121 implementation = _http_archive_version_impl ,
69122 attrs = {
123+ "bzlmod" : attr .bool (doc = "Whether we were called from a bzlmod module extension" ),
124+ "check_for_updates" : attr .bool (doc = "Whether to check for a newer version of rules_ts" ),
70125 "integrity" : attr .string (doc = "Needed only if the ts version isn't mirrored in `versions.bzl`." ),
71126 "version" : attr .string (doc = "Explicit version for `urls` placeholder. If provided, the package.json is not read." ),
72127 "urls" : attr .string_list (doc = "URLs to fetch from. Each must have one `{}`-style placeholder." ),
@@ -77,13 +132,15 @@ http_archive_version = repository_rule(
77132)
78133
79134# buildifier: disable=function-docstring
80- def npm_dependencies (ts_version_from = None , ts_version = None , ts_integrity = None ):
135+ def npm_dependencies (ts_version_from = None , ts_version = None , ts_integrity = None , bzlmod = False , check_for_updates = True ):
81136 if (ts_version and ts_version_from ) or (not ts_version_from and not ts_version ):
82137 fail ("""Exactly one of 'ts_version' or 'ts_version_from' must be set.""" )
83138
84139 maybe (
85140 http_archive_version ,
86141 name = "npm_typescript" ,
142+ bzlmod = bzlmod ,
143+ check_for_updates = check_for_updates ,
87144 version = ts_version ,
88145 version_from = ts_version_from ,
89146 integrity = ts_integrity ,
0 commit comments