├── .gitignore ├── LICENSE ├── README.md ├── rebar.config └── src ├── rebar_raw_resource.app.src └── rebar_raw_resource.erl /.gitignore: -------------------------------------------------------------------------------- 1 | # rebar3 and tools 2 | /.cache/ 3 | /.rebar3/ 4 | /_build/ 5 | /rebar.lock 6 | 7 | # build artifacts 8 | /ebin/ 9 | *.app 10 | *.beam 11 | 12 | # editor droppings 13 | *.bak 14 | *~ 15 | 16 | # work environments 17 | *.iml 18 | .DS_Store 19 | .idea/ 20 | .project 21 | .settings/ 22 | .tm_properties 23 | local.*.plt 24 | Makefile 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #`raw` Resource Provider for Rebar3 2 | 3 | [Rebar3][rebar3] requires that all dependencies be structured as OTP applications. 4 | There are, however, lots of cases when you just want to include a dependency in your project from which to pull unstructured resources, whether source code, graphics, text, or any number of other possibilities. 5 | This `rebar3` plugin allows you to do just that. 6 | 7 | ##What It Does 8 | 9 | This resource provider does two things: 10 | 11 | * Wraps dependency specifications for other resource providers, leveraging their abilities to retrieve and provide information about their target dependencies. 12 | 13 | * Creates a minimal OTP application structure in retrieved dependencies that satisfies rebar's structure requirements. 14 | 15 | ##How to Use It 16 | 17 | Everything the plugin does is configured in your project's `rebar.config` file. 18 | 19 | What you do with the dependency once you get it into rebar's build tree is up to you, but `rebar3` has a _(somewhat)_ documented set of environment variables that allow you to find it so you can do with it what you need. The variable `$REBAR_DEPS_DIR`, in particular, points to the directory under which you'll find the downloaded dependency. 20 | 21 | _You'll find some information in the [rebar3 configuration][rebar3cfg] documentation, but you may need to run `rebar3` with `DEBUG` set and/or review the [source][rebar3src] to find all of the accessible information._ 22 | 23 | ###Add The Plugin 24 | 25 | The following addition to `rebar.config` makes the plugin available for use with your dependencies: 26 | 27 | ```erlang 28 | {plugins, [ 29 | {rebar_raw_resource, 30 | {git, "git://github.com/basho/rebar_raw_resource.git", 31 | {branch, "master"}}} 32 | ]}. 33 | ``` 34 | 35 | ###Add Raw Dependencies 36 | 37 | The `raw` resource type is simply a wrapper around another type, so the basic structure of a raw dependency is: 38 | 39 | ```erlang 40 | {deps, [ 41 | . . . 42 | {mydep1, {raw, {mydep1-resource-specification} }}, 43 | {mydep2, {raw, {mydep2-resource-specification}, [raw-options] }}, 44 | . . . 45 | ]}. 46 | ``` 47 | 48 | In the above, the _resource-specification_ tuples are as described in Rebar's [documentation][depdocs], and might look like this: 49 | 50 | ```erlang 51 | {git, "git://some/git/repo.git", {branch, "master"}} 52 | ``` 53 | or 54 | ```erlang 55 | {hg, "https://some/mercurial/repo"} 56 | ``` 57 | 58 | The `raw` resource doesn't know anything about other resource types, it just uses their specifications to call into other resource providers to fetch and report on the dependencies. 59 | 60 | ####Dependency Options 61 | 62 | You can optionally include a description and/or version in a `raw` dependency specification which will affect the generated `.app` file. 63 | By default, for a dependency named `foo` that has neither a `src/foo.app.src` or `ebin/foo.app` file, a `ebin/foo.app` file will be written that looks like this: 64 | 65 | ```erlang 66 | %% 67 | %% Generated by rebar_raw_resource 68 | %% 69 | {application, foo, 70 | [ 71 | {description, "foo"}, 72 | {vsn, "some-version-string"}, 73 | {modules, []}, 74 | {registered, []}, 75 | {applications, [kernel, stdlib]} 76 | ]}. 77 | ``` 78 | 79 | The `description` field is simply the dependency's name, and the `vsn` field is whatever the underlying resource provider returns from the downloaded dependency. You can override these values by providing your own as in the following: 80 | 81 | ```erlang 82 | {deps, [ 83 | . . . 84 | {snappy, {raw, {git, "git://github.com/google/snappy.git", {branch, "master"}}, 85 | [ {description, "Google's Snappy compression library"}, 86 | {vsn, "probably-not-a-good-idea-to-override-this"} ] }} 87 | . . . 88 | ]}. 89 | ``` 90 | 91 | #####A Note About Dependency Options 92 | 93 | There are probably very few, if any, legitimate use cases for these dependency options. 94 | If the dependency is going to be included in your project as an OTP application, you should almost certainly craft a more appropriate `.app` or `.app.src` file. 95 | 96 | ##Caveats 97 | 98 | ###Scope of Dependency Names and Locations 99 | 100 | At present, I don't discriminate between scopes, assuming a given dependency location always maps to the same name. 101 | Including the dependency's version selector and profile would allow complete uniqueness of mappings, but subsequent operations may alter the version selector, resulting in matches not being found. 102 | Overall, I think it's reasonable to have a constraint saying _"You **MUST** use the same name for a given dependency URL across profiles"_ - in fact, that constraint may also be implicit in rebar3 itself, though it doesn't appear to be formally declared. 103 | 104 | Because the nested resource specifiers (rightly) don't have a specified schema, we treat them as relatively opaque tuples where the first element is the type and the second element is the location. 105 | 106 | Old-style dependencies with a version regex immediately following the dependency name SHOULD be accommodated but ignored, though this aspect hasn't been, and isn't likely to be, rigorously tested as it's moot in rebar3. 107 | 108 | ###Dependency Source Tree Modification 109 | 110 | Currently, we satisfy rebar's need for an OTP application by scribbling a minimal app config file in `dep-path/ebin/dep-name.app` if there's neither such a file already, nor its equivalent source in the `dep-path/src` directory. If anything that will satisfy rebar3's OTP application requirement is found, the plugin won't write any file. 111 | 112 | It would be preferable to build the `rebar_app_info` record dynamically and insert it into the state so we didn't have to leave droppings in the dependency's filesystem, but it's unclear whether there's a reliable place to hook that functionality into the processing sequence or whether rebar would accept it if we did without an actual file to check for changes. 113 | 114 | Note that the above means that if you go into the dependency's source directory and check its status it will appear to have pending changes, but you probably don't want to commit the generated file. 115 | 116 | ##Enhancements 117 | 118 | If you find a bug, please report an [issue][issues], or better yet, fix it and submit a [pull request][pulls]. 119 | 120 | You _may_ find that using the `develop` branch here fixes a problem you're having, but it's just as likely that it'll crash and burn, so unless you're comfortable reviewing the code yourself you should probably stay away from it. 121 | 122 | If there's something to discuss, especially if others might be interested, the rebar [mailing list][discuss] is the place to go. 123 | 124 | ##License 125 | 126 | Everything here is covered by this [license][]. 127 | 128 | 129 | [depdocs]: https://www.rebar3.org/docs/dependencies 130 | [discuss]: http://lists.basho.com/mailman/listinfo/rebar_lists.basho.com 131 | [issues]: https://github.com/basho/rebar_raw_resource/issues 132 | [license]: LICENSE 133 | [pulls]: https://github.com/basho/rebar_raw_resource/pulls 134 | [rebar3]: https://www.rebar3.org 135 | [rebar3cfg]: https://www.rebar3.org/docs/configuration 136 | [rebar3src]: https://github.com/erlang/rebar3 137 | 138 | -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | %% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*- 2 | %% ------------------------------------------------------------------- 3 | %% 4 | %% Copyright (c) 2016 Basho Technologies, Inc. 5 | %% 6 | %% This file is provided to you under the Apache License, 7 | %% Version 2.0 (the "License"); you may not use this file 8 | %% except in compliance with the License. You may obtain 9 | %% a copy of the License at 10 | %% 11 | %% http://www.apache.org/licenses/LICENSE-2.0 12 | %% 13 | %% Unless required by applicable law or agreed to in writing, 14 | %% software distributed under the License is distributed on an 15 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | %% KIND, either express or implied. See the License for the 17 | %% specific language governing permissions and limitations 18 | %% under the License. 19 | %% 20 | %% ------------------------------------------------------------------- 21 | 22 | {profiles, [ 23 | {prod, [ 24 | {erl_opts, [ 25 | no_debug_info, 26 | no_auto_import, 27 | warn_export_all, 28 | warn_obsolete_guard, 29 | warnings_as_errors 30 | ]} 31 | ]}, 32 | {develop, [ 33 | {erl_opts, [ 34 | debug_info, 35 | {d, rrr_develop}, 36 | no_auto_import, 37 | warn_export_all, 38 | warn_obsolete_guard, 39 | warnings_as_errors 40 | ]}, 41 | % Running dialyzer on a rebar plugin from within rebar causes all 42 | % sorts of suffering, so I run it externally and don't bother with 43 | % a configuration clause for it here at all. 44 | 45 | % Xref, on the other hand, is usable as long as rebar is invoked by 46 | % the emulator directly, which I do in development, so setting it up 47 | % here is still simpler than running it externally. 48 | {xref_checks, [ 49 | deprecated_function_calls, 50 | deprecated_functions, 51 | locals_not_used, 52 | undefined_function_calls, 53 | undefined_functions 54 | ]} 55 | ]} 56 | ]}. 57 | -------------------------------------------------------------------------------- /src/rebar_raw_resource.app.src: -------------------------------------------------------------------------------- 1 | %% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*- 2 | %% ------------------------------------------------------------------- 3 | %% 4 | %% Copyright (c) 2016 Basho Technologies, Inc. 5 | %% 6 | %% This file is provided to you under the Apache License, 7 | %% Version 2.0 (the "License"); you may not use this file 8 | %% except in compliance with the License. You may obtain 9 | %% a copy of the License at 10 | %% 11 | %% http://www.apache.org/licenses/LICENSE-2.0 12 | %% 13 | %% Unless required by applicable law or agreed to in writing, 14 | %% software distributed under the License is distributed on an 15 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | %% KIND, either express or implied. See the License for the 17 | %% specific language governing permissions and limitations 18 | %% under the License. 19 | %% 20 | %% ------------------------------------------------------------------- 21 | 22 | {application, rebar_raw_resource, 23 | [ 24 | {description, "Raw resource provider for rebar3"}, 25 | {vsn, "git"}, 26 | {modules, []}, 27 | {registered, []}, 28 | {applications, [kernel, stdlib]} 29 | ]}. 30 | -------------------------------------------------------------------------------- /src/rebar_raw_resource.erl: -------------------------------------------------------------------------------- 1 | %% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*- 2 | %% ------------------------------------------------------------------- 3 | %% 4 | %% Copyright (c) 2016 Basho Technologies, Inc. 5 | %% 6 | %% This file is provided to you under the Apache License, 7 | %% Version 2.0 (the "License"); you may not use this file 8 | %% except in compliance with the License. You may obtain 9 | %% a copy of the License at 10 | %% 11 | %% http://www.apache.org/licenses/LICENSE-2.0 12 | %% 13 | %% Unless required by applicable law or agreed to in writing, 14 | %% software distributed under the License is distributed on an 15 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | %% KIND, either express or implied. See the License for the 17 | %% specific language governing permissions and limitations 18 | %% under the License. 19 | %% 20 | %% ------------------------------------------------------------------- 21 | 22 | -module(rebar_raw_resource). 23 | % 24 | % For efficiency in production use, we don't have a dependency on rebar 25 | % itself, so the behaviors this module implements aren't always visible. 26 | % 27 | -ifdef(rrr_develop). 28 | -behaviour(provider). 29 | -behaviour(rebar_resource). 30 | -endif. 31 | 32 | % the dependency resource type identifier associated with this resource. 33 | -define(RTYPE, raw). 34 | 35 | -export([ 36 | % provider 37 | do/1, 38 | format_error/1, 39 | init/1, 40 | % rebar_resource 41 | download/3, 42 | lock/2, 43 | make_vsn/1, 44 | needs_update/2 45 | ]). 46 | 47 | % For development only - you *really* don't want this defined! 48 | %-define(RRR_DEBUG, true). 49 | 50 | -define(else, 'true'). 51 | -define(is_min_tuple(Var, Min), 52 | erlang:is_tuple(Var) andalso erlang:tuple_size(Var) >= Min). 53 | -define(is_rec_type(Var, Type, Min), 54 | ?is_min_tuple(Var, Min) andalso erlang:element(1, Var) =:= Type). 55 | -define(is_rec_type(Var, Type), ?is_rec_type(Var, Type, 1)). 56 | -define(mod_error(Error), {'error', {?MODULE, Err}}). 57 | 58 | -ifdef(RRR_DEBUG). 59 | -define(RRR_STATE(Field), debug_state(State, Field)). 60 | -define(throw_error(Error), debug_call_stack(), erlang:error({?MODULE, Error})). 61 | -else. 62 | -define(RRR_STATE(Field), 'ok'). 63 | -define(throw_error(Error), erlang:error({?MODULE, Error})). 64 | -endif. 65 | 66 | % 67 | % Implementation Notes: 68 | % 69 | % ALL functions are spec'd! 70 | % Specs for exported functions MAY be more specialized than their specifying 71 | % behaviors. 72 | % 73 | % We maintain mappings in the process dictionary between: 74 | % 75 | % - resource type => implementing module 76 | % - dependency name => resource type and repository location 77 | % - repository location => resource type and dependency name 78 | % 79 | % These mappings are updated whenever we have access to a rebar_state 80 | % record, which isn't available during any of the resource operations other 81 | % than download/3. Worse, if the dependency is already in place, the non-state 82 | % needs_update/2 operation will be the first called on it, so we have to have 83 | % set up our mapping before that. That leaves plugin initialization as the 84 | % only chance to get the mappings set up before they're needed. 85 | % 86 | % At present, I don't discriminate between scopes, assuming a given dependency 87 | % location always maps to the same name. Including the dependency's version 88 | % selector and profile would allow complete uniqueness of mappings, but 89 | % subsequent operations may alter the version selector, resulting in matches 90 | % not being found. Overall, I think it's reasonable to have a constraint saying 91 | % 'You MUST use the same name for a given dependency URL across profiles.' 92 | % 93 | % Because the nested resource specifiers (rightly) don't have a specified 94 | % schema, we treat them as relatively opaque tuples where the first element is 95 | % the type and the second element is the location. 96 | % Old-style dependencies with a version regex immediately following the 97 | % dependency name SHOULD be accommodated but ignored, though this aspect hasn't 98 | % been, and isn't likely to be, rigorously tested as it's moot in rebar3. 99 | % 100 | % Currently, we satisfy rebar's need for an OTP application by scribbling a 101 | % minimal app config file in /ebin/.app if there's not 102 | % such a file already, or its equivalent source in the /src 103 | % directory. 104 | % 105 | % It would be preferable to build the rebar_app_info record dynamically and 106 | % insert it into the state so we didn't have to leave droppings in the 107 | % dependency's filesystem, but some processing sequences in rebar access the 108 | % actual file to check for changes, so it looks like the current behavior has 109 | % to stay for the time being. 110 | % 111 | 112 | %% =================================================================== 113 | %% Types - none exported 114 | %% =================================================================== 115 | % 116 | % Type names have prefixes of 'rsrc_' indicating they apply to all dependency 117 | % specs or 'this_' indicating they're specific to this resource type. 118 | % 119 | -ifdef(rrr_develop). 120 | -type rebar_lock() :: rebar_resource:resource(). 121 | -type rebar_state() :: rebar_state:t(). 122 | -type rsrc_loc() :: rebar_resource:location(). 123 | -type rsrc_ref() :: rebar_resource:ref(). 124 | -type rsrc_type() :: rebar_resource:type(). 125 | -else. 126 | -type rebar_lock() :: {rsrc_type(), rsrc_loc(), rsrc_ref()}. 127 | -type rebar_state() :: tuple(). 128 | -type rsrc_loc() :: string(). % URL-ish 129 | -type rsrc_ref() :: term(). % repo version specifier 130 | -type rsrc_type() :: atom(). 131 | -endif. 132 | -type keyable() :: atom() | binary() | list(). 133 | 134 | -type rebar_dep() :: {rsrc_name()} | {rsrc_name(), rsrc_spec() | rsrc_vsn()}. 135 | -type rebar_dir() :: file:filename_all(). 136 | -type rebar_err() :: {'error', term()}. 137 | -type rebar_rsrc() :: {rsrc_type(), rsrc_mod()}. 138 | -type rebar_vsn() :: {'plain', rsrc_vsn()}. 139 | 140 | -type rsrc_dir() :: rebar_dir(). 141 | -type rsrc_mod() :: module(). 142 | -type rsrc_name() :: atom(). 143 | -type rsrc_spec() :: tuple(). % {rsrc_type(), rsrc_loc(), ...} 144 | -type rsrc_vsn() :: string(). 145 | 146 | -type this_opt() :: atom() | {atom(), term()}. 147 | -type this_opts() :: [this_opt()]. 148 | -type this_spec() :: {this_type(), rsrc_spec()} 149 | | {this_type(), rsrc_spec(), this_opts()} 150 | | {this_type(), rsrc_loc(), mod_ref()}. 151 | -type this_type() :: ?RTYPE. 152 | 153 | %-type map_key() :: name_key() | spec_key() | type_key(). 154 | %%-type name_key() :: {?MODULE, 'name', rsrc_name()}. % => rsrc_type() 155 | %%-type spec_key() :: {?MODULE, 'spec', rsrc_loc()}. % => rsrc_name() 156 | %%-type type_key() :: {?MODULE, 'type', rsrc_type()}. % => module() 157 | 158 | -record(mod_ref, { 159 | res :: rsrc_type(), 160 | ref :: rsrc_ref(), 161 | opt :: this_opts() 162 | }). 163 | -type mod_ref() :: #mod_ref{}. 164 | 165 | -record(mod_dep, { 166 | name :: rsrc_name(), 167 | res :: rsrc_type(), 168 | loc :: rsrc_loc() 169 | }). 170 | -type mod_dep() :: #mod_dep{}. 171 | 172 | -record(mod_res, { 173 | res :: rsrc_type(), 174 | mod :: rsrc_mod() 175 | }). 176 | -type mod_res() :: #mod_res{}. 177 | 178 | -record(mod_data, { 179 | rsrcs = [] :: [mod_res()], 180 | deps = [] :: [mod_dep()] 181 | }). 182 | -type mod_data() :: #mod_data{}. 183 | 184 | %% =================================================================== 185 | %% Provider API 186 | %% =================================================================== 187 | 188 | -spec init(State :: rebar_state()) -> {'ok', rebar_state()}. 189 | % 190 | % Installs the resource handler, the provider itself does nothing. 191 | % 192 | % This gets called repeatedly, for each profile, and in each case we want 193 | % to prime the process environment with any info we may need later, as 194 | % download/3 is NOT called first if the dependency is already present, and 195 | % it's the only resource call that gets to see the rebar state. 196 | % 197 | init(State) -> 198 | #mod_data{} = absorb_state(State), 199 | {'ok', rebar_state:add_resource(State, {?RTYPE, ?MODULE})}. 200 | 201 | -spec do(State :: rebar_state:t()) -> {'ok', rebar_state()}. 202 | % 203 | % Fulfills the `provider' contract, does nothing ... for now. 204 | % 205 | % IF there's a viable way to hook rebar's app_discovery provider, this will 206 | % be the place to do it, but it looks like rebar really wants to see a physical 207 | % file so there's not much we can do. 208 | % 209 | do(State) -> 210 | {'ok', State}. 211 | 212 | -spec format_error(Error :: term()) -> iolist(). 213 | % 214 | % Converts specified Error to a string. 215 | % Most errors in this module are in the form {atom(), term()} and are handled 216 | % down in the internal format_error/4 function. 217 | % 218 | format_error({Class, Data}) -> 219 | format_error('io_lib', 'format', Class, Data); 220 | 221 | format_error(Error) -> 222 | io_lib:format("~p", [Error]). 223 | 224 | %% =================================================================== 225 | %% Resource API 226 | %% =================================================================== 227 | 228 | -spec download( 229 | Dest :: rsrc_dir(), From :: this_spec(), State :: rebar_state()) 230 | -> {'ok', term()} | rebar_err(). 231 | % 232 | % Download the specified resource using its underlying handler. 233 | % 234 | download(Dest, {?RTYPE, Loc, 235 | #mod_ref{res = Res, ref = Ref, opt = Opts}}, State) -> 236 | download(Dest, {?RTYPE, {Res, Loc, Ref}, Opts}, State); 237 | 238 | download(Dest, {?RTYPE, Spec}, State) -> 239 | download(Dest, {?RTYPE, Spec, []}, State); 240 | 241 | download(Dest, {?RTYPE, Spec, Opts}, State) -> 242 | ?RRR_STATE('state'), 243 | 244 | Data = absorb_state(State), 245 | {Res, Loc} = parse_ext_spec(Spec), 246 | % Do both lookups before calling the handler's download to get the 247 | % exception out of the way if it's coming. 248 | #mod_res{mod = Mod} = lookup_res(Data, Res), 249 | #mod_dep{name = Name} = lookup_loc(Data, Loc), 250 | case Mod:download(Dest, Spec, State) of 251 | {'ok', _} = Ret -> 252 | ensure_app(Dest, Mod, Name, Opts, Ret); 253 | Err -> 254 | Err 255 | end. 256 | 257 | -spec lock(Path :: rsrc_dir(), Spec :: this_spec()) 258 | -> rebar_lock() | no_return(). 259 | % 260 | % Pass through to the underlying resource handler. 261 | % Note that the callback doesn't allow an error tuple to be returned, so an 262 | % exception is our only option if we can't look up the mapping. 263 | % 264 | lock(Path, {?RTYPE, Loc, #mod_ref{res = Res, ref = Prev} = Rec}) -> 265 | #mod_res{mod = Mod} = lookup_res(mod_data(), Res), 266 | {Res, Loc, Ref} = Mod:lock(Path, {Res, Loc, Prev}), 267 | {?RTYPE, Loc, Rec#mod_ref{ref = Ref}}; 268 | 269 | lock(Path, {?RTYPE, Spec}) -> 270 | lock(Path, {?RTYPE, Spec, []}); 271 | 272 | lock(Path, {?RTYPE, Spec, Opts}) -> 273 | {Res, _} = parse_ext_spec(Spec), 274 | #mod_res{mod = Mod} = lookup_res(mod_data(), Res), 275 | {Res, Loc, Ref} = Mod:lock(Path, Spec), 276 | {?RTYPE, Loc, #mod_ref{res = Res, ref = Ref, opt = Opts}}. 277 | 278 | -spec needs_update(Path :: rsrc_dir(), Spec :: this_spec()) 279 | -> boolean() | no_return(). 280 | % 281 | % Pass through to the underlying resource handler. 282 | % Note that the callback doesn't allow an error tuple to be returned, so an 283 | % exception is our only option if we can't look up the mapping. 284 | % 285 | needs_update(Path, {?RTYPE, Loc, #mod_ref{res = Res, ref = Ref}}) -> 286 | #mod_res{mod = Mod} = lookup_res(mod_data(), Res), 287 | Mod:needs_update(Path, {Res, Loc, Ref}); 288 | 289 | needs_update(Path, {?RTYPE, Spec, _}) -> 290 | needs_update(Path, {?RTYPE, Spec}); 291 | 292 | needs_update(Path, {?RTYPE, Spec}) -> 293 | {Res, _} = parse_ext_spec(Spec), 294 | #mod_res{mod = Mod} = lookup_res(mod_data(), Res), 295 | Mod:needs_update(Path, Spec). 296 | 297 | -spec make_vsn(Path :: rsrc_dir()) 298 | -> rebar_vsn() | {'error', string()} | no_return(). 299 | % 300 | % Pass through to the underlying resource handler. 301 | % The weird error tuple spec comes from the rebar_resource behavior. 302 | % 303 | make_vsn(Path) -> 304 | Data = mod_data(), 305 | #mod_dep{res = Res} = lookup_dep(Data, path_name(Path)), 306 | #mod_res{mod = Mod} = lookup_res(Data, Res), 307 | Mod:make_vsn(Path). 308 | 309 | %% =================================================================== 310 | %% Internal 311 | %% =================================================================== 312 | 313 | -spec format_error( 314 | Mod :: module(), Func :: atom(), Class :: term(), Data :: term()) 315 | -> term(). 316 | % 317 | % Assume Mod:Func/2 accepts the same arguments as io_lib:format/2. 318 | % The rebar_api:/2 functions meet this requirement, so we can define 319 | % the error translations in one place (here) and use it throughout the module. 320 | % Since we don't know what function is being called, we don't know what it 321 | % returns, but the caller does. 322 | % 323 | format_error(Mod, Func, 'duplicate_dep_name', Name) -> 324 | Mod:Func("Conflicting definitions of dependency '~s'", [Name]); 325 | format_error(Mod, Func, 'duplicate_dep_loc', Loc) -> 326 | Mod:Func("Conflicting dependencies at '~s'", [Loc]); 327 | format_error(Mod, Func, 'duplicate_res_type', Res) -> 328 | Mod:Func("Conflicting '~s' resource definitions", [Res]); 329 | format_error(Mod, Func, 'unmapped_dep_name', Name) -> 330 | Mod:Func("Unmapped dependency name '~s'", [Name]); 331 | format_error(Mod, Func, 'unmapped_dep_loc', Loc) -> 332 | Mod:Func("Unmapped dependency location '~s'", [Loc]); 333 | format_error(Mod, Func, 'unmapped_res_type', Res) -> 334 | Mod:Func("Unmapped resource type '~s'", [Res]); 335 | format_error(Mod, Func, 'unrecognized_ext_spec', Spec) -> 336 | Mod:Func("Unrecognized dependency structure: ~p", [Spec]); 337 | format_error(Mod, Func, Class, Data) -> 338 | Mod:Func("~p:~p", [Class, Data]). 339 | 340 | % The absorb_... functions soak up whatever we care about from the state. 341 | % 342 | % There's a lot of code here that may be redundant and/or unused as I feel 343 | % around trying to make sure all of the dependencies are found in each runtime 344 | % scenario. 345 | % Hopefully it'll all be cleaned up someday. 346 | 347 | -spec absorb_state(State :: rebar_state()) -> mod_data() | no_return(). 348 | absorb_state(State) -> 349 | Data1 = mod_data(), 350 | Data2 = absorb_resources(Data1, rebar_state:resources(State)), 351 | Data3 = absorb_deps(Data2, rebar_state:get(State, 'deps', [])), 352 | Data4 = absorb_app_infos(Data3, rebar_state:lock(State)), 353 | % Data5 = absorb_profiles(Data4, rebar_state:current_profiles(State), State), 354 | mod_data(dump_mod_data(Data4)). 355 | 356 | -spec absorb_app_infos(Data :: mod_data(), AppInfos :: [tuple()]) 357 | -> mod_data() | no_return(). 358 | absorb_app_infos(Data, [AppInfo | AppInfos]) -> 359 | absorb_app_infos( 360 | absorb_dep(Data, 361 | rebar_app_info:name(AppInfo), rebar_app_info:source(AppInfo)), 362 | AppInfos); 363 | absorb_app_infos(Data, []) -> 364 | Data. 365 | 366 | -spec absorb_deps(Data :: mod_data(), Deps :: [rebar_dep()]) 367 | -> mod_data() | no_return(). 368 | absorb_deps(Data, [Dep | Deps]) -> 369 | absorb_deps(absorb_dep(Data, Dep), Deps); 370 | absorb_deps(Data, []) -> 371 | Data. 372 | 373 | -ifdef(rrr_develop). 374 | -compile({nowarn_unused_function, absorb_profiles/3}). 375 | -spec absorb_profiles( 376 | Data :: mod_data(), Profiles :: [atom()], State :: rebar_state()) 377 | -> mod_data() | no_return(). 378 | absorb_profiles(Data, [Profile | Profiles], State) -> 379 | absorb_profiles( 380 | absorb_app_infos( 381 | Data, rebar_state:get(State, {'parsed_deps', Profile}, [])), 382 | Profiles, State); 383 | absorb_profiles(Data, [], _) -> 384 | Data. 385 | -endif. 386 | 387 | -spec absorb_resources( 388 | Data :: mod_data(), Resources :: [rebar_rsrc()]) 389 | -> mod_data() | no_return(). 390 | % 391 | % Allow for whatever may come through to handle future extensions. 392 | % 393 | absorb_resources(Data, [Res | Resources]) when ?is_min_tuple(Res, 2) -> 394 | absorb_resources( 395 | map_res(Data, #mod_res{ 396 | res = term_to_atom(erlang:element(1, Res)), 397 | mod = erlang:element(2, Res)}), 398 | Resources); 399 | absorb_resources(Data, [_ | Resources]) -> 400 | absorb_resources(Data, Resources); 401 | absorb_resources(Data, []) -> 402 | Data. 403 | 404 | -spec absorb_dep(Data :: mod_data(), Spec :: rsrc_spec()) 405 | -> mod_data() | no_return(). 406 | % 407 | % Accommodate dependencies with or without the rebar2 regex string. 408 | % Be as lenient as we can about current and future structure. 409 | % 410 | absorb_dep(Data, Dep) when ?is_min_tuple(Dep, 2) 411 | andalso erlang:is_tuple(erlang:element(2, Dep)) -> 412 | absorb_dep(Data, erlang:element(1, Dep), erlang:element(2, Dep)); 413 | absorb_dep(Data, Dep) when ?is_min_tuple(Dep, 3) 414 | andalso erlang:is_tuple(erlang:element(3, Dep)) -> 415 | absorb_dep(Data, erlang:element(1, Dep), erlang:element(3, Dep)); 416 | absorb_dep(Data, _) -> 417 | Data. 418 | 419 | -spec absorb_dep(Data :: mod_data(), Name :: keyable(), Spec :: rsrc_spec()) 420 | -> mod_data() | no_return(). 421 | absorb_dep(Data, Name, {?RTYPE, Loc, #mod_ref{res = Res}}) -> 422 | map_dep(Data, #mod_dep{ 423 | name = term_to_atom(Name), res = term_to_atom(Res), loc = Loc}); 424 | absorb_dep(Data, Name, Spec) when ?is_rec_type(Spec, ?RTYPE, 2) -> 425 | {Res, Loc} = parse_ext_spec(erlang:element(2, Spec)), 426 | map_dep(Data, #mod_dep{name = term_to_atom(Name), res = Res, loc = Loc}); 427 | absorb_dep(Data, _, _) -> 428 | Data. 429 | 430 | -spec ensure_app( 431 | Path :: rsrc_dir(), 432 | Mod :: module(), 433 | Name :: atom(), 434 | Opts :: this_opts(), 435 | Result :: {'ok', term()} ) 436 | -> {'ok', term()} | rebar_err(). 437 | % 438 | % Make sure there's something rebar will consider to be an app in the 439 | % directory specified by Path. 440 | % The return value is as specified for download/3 - Result on success or an 441 | % 'error' tuple otherwise. 442 | % 443 | ensure_app(Path, Mod, Name, Opts, Result) -> 444 | BApp = lists:flatten(filename:join( 445 | [Path, "ebin", io_lib:format("~s.app", [Name])])), 446 | SApp = lists:flatten(filename:join( 447 | [Path, "src", io_lib:format("~s.app.src", [Name])])), 448 | case filelib:is_file(BApp) orelse filelib:is_file(SApp) of 449 | 'true' -> 450 | Result; 451 | _ -> 452 | Vsn = case proplists:get_value('vsn', Opts) of 453 | 'undefined' -> 454 | {'plain', Val} = Mod:make_vsn(Path), 455 | Val; 456 | Val -> 457 | Val 458 | end, 459 | Desc = proplists:get_value('description', Opts, Name), 460 | Data = io_lib:format( 461 | "%%\n" 462 | "%% Generated by ~s\n" 463 | "%%\n" 464 | % this is the minimum set of elements required to make rebar 465 | % happy when there are no sources for it to compile 466 | "{application, ~s,\n" 467 | "[\n" 468 | " {description, \"~s\"},\n" 469 | " {vsn, \"~s\"},\n" 470 | " {modules, []},\n" 471 | " {registered, []},\n" 472 | " {applications, [kernel, stdlib]}\n" 473 | "]}.\n", 474 | [?MODULE, Name, Desc, Vsn]), 475 | case filelib:ensure_dir(BApp) of 476 | 'ok' -> 477 | case file:write_file(BApp, Data) of 478 | 'ok' -> 479 | Result; 480 | Err -> 481 | Err 482 | end; 483 | Err -> 484 | Err 485 | end 486 | end. 487 | 488 | -spec parse_ext_spec(Spec :: rsrc_spec()) 489 | -> {rsrc_type(), rsrc_loc()} | no_return(). 490 | parse_ext_spec(Spec) when ?is_min_tuple(Spec, 2) -> 491 | {term_to_atom(erlang:element(1, Spec)), erlang:element(2, Spec)}; 492 | parse_ext_spec(Spec) -> 493 | Class = 'unrecognized_ext_spec', 494 | format_error('rebar_api', 'error', Class, Spec), 495 | ?throw_error({Class, Spec}). 496 | 497 | -spec path_name(Path :: rsrc_dir()) -> rsrc_name() | rebar_err(). 498 | % 499 | % Get the resource name from the specified dependency path. 500 | % 501 | path_name(Path) -> 502 | term_to_atom(lists:last(filename:split(Path))). 503 | 504 | -spec term_to_atom(Term :: keyable()) -> atom() | no_return(). 505 | % 506 | % make the specified term into an atom 507 | % 508 | term_to_atom(Term) when erlang:is_atom(Term) -> 509 | Term; 510 | term_to_atom(Term) when erlang:is_binary(Term) -> 511 | erlang:binary_to_atom(Term, 'latin1'); 512 | term_to_atom(Term) when erlang:is_list(Term) -> 513 | erlang:list_to_atom(Term); 514 | term_to_atom(Term) -> 515 | erlang:error('badarg', [Term]). 516 | 517 | -ifdef(rrr_develop). 518 | -compile({nowarn_unused_function, term_to_binary/1}). 519 | -spec term_to_binary(Term :: keyable()) -> binary() | no_return(). 520 | % 521 | % make the specified term into a binary 522 | % 523 | term_to_binary(Term) when erlang:is_atom(Term) -> 524 | erlang:atom_to_binary(Term, 'latin1'); 525 | term_to_binary(Term) when erlang:is_binary(Term) -> 526 | Term; 527 | term_to_binary(Term) when erlang:is_list(Term) -> 528 | erlang:list_to_binary(Term); 529 | term_to_binary(Term) -> 530 | erlang:error('badarg', [Term]). 531 | -endif. 532 | 533 | % 534 | % Module state functions. 535 | % Rebar doesn't give us consistent access to modifiable state, so we keep it 536 | % in the process environment. 537 | % 538 | -define(MOD_DATA_KEY, {?MODULE, 'data'}). 539 | 540 | -spec mod_data() -> mod_data(). 541 | mod_data() -> 542 | case erlang:get(?MOD_DATA_KEY) of 543 | 'undefined' -> 544 | #mod_data{}; 545 | #mod_data{} = Data -> 546 | Data 547 | end. 548 | 549 | -spec mod_data(Data :: mod_data()) -> mod_data(). 550 | mod_data(#mod_data{} = Data) -> 551 | erlang:put(?MOD_DATA_KEY, Data), 552 | Data. 553 | 554 | -spec map_dep(Data :: mod_data(), Dep :: mod_dep()) -> mod_data() | no_return(). 555 | map_dep(#mod_data{deps = Deps} = Data, 556 | #mod_dep{name = Name, loc = Loc} = Dep) -> 557 | case lists:keyfind(Name, #mod_dep.name, Deps) of 558 | Dep -> 559 | Data; 560 | #mod_dep{} -> 561 | Class = 'duplicate_dep_name', 562 | format_error('rebar_api', 'error', Class, Name), 563 | _ = dump_mod_data(Data), 564 | ?throw_error({Class, Name}); 565 | 'false' -> 566 | case lists:keyfind(Loc, #mod_dep.loc, Deps) of 567 | #mod_dep{} -> 568 | Class = 'duplicate_dep_loc', 569 | format_error('rebar_api', 'error', Class, Loc), 570 | _ = dump_mod_data(Data), 571 | ?throw_error({Class, Loc}); 572 | 'false' -> 573 | Data#mod_data{deps = [Dep | Deps]} 574 | end 575 | end. 576 | 577 | -spec map_res(Data :: mod_data(), Res :: mod_res()) -> mod_data() | no_return(). 578 | map_res(#mod_data{rsrcs = Rsrcs} = Data, #mod_res{res = Type} = Res) -> 579 | case lists:keyfind(Type, #mod_res.res, Rsrcs) of 580 | Res -> 581 | Data; 582 | #mod_res{} -> 583 | Class = 'duplicate_res_type', 584 | format_error('rebar_api', 'error', Class, Type), 585 | _ = dump_mod_data(Data), 586 | ?throw_error({Class, Type}); 587 | 'false' -> 588 | Data#mod_data{rsrcs = [Res | Rsrcs]} 589 | end. 590 | 591 | -spec lookup_dep(Data :: mod_data(), DepName :: keyable()) 592 | -> mod_dep() | no_return(). 593 | % 594 | % Accommodate DepName being something other than an atom, for example the 595 | % 'name' field in an app_info record. 596 | % 597 | lookup_dep(#mod_data{deps = Deps} = Data, DepName) -> 598 | Name = term_to_atom(DepName), 599 | case lists:keyfind(Name, #mod_dep.name, Deps) of 600 | #mod_dep{} = Dep -> 601 | Dep; 602 | _ -> 603 | Class = 'unmapped_dep_name', 604 | format_error('rebar_api', 'error', Class, Name), 605 | _ = dump_mod_data(Data), 606 | ?throw_error({Class, Name}) 607 | end. 608 | 609 | -spec lookup_loc(Data :: mod_data(), Loc :: rsrc_loc()) 610 | -> mod_dep() | no_return(). 611 | lookup_loc(#mod_data{deps = Deps} = Data, Loc) -> 612 | case lists:keyfind(Loc, #mod_dep.loc, Deps) of 613 | #mod_dep{} = Dep -> 614 | Dep; 615 | _ -> 616 | Class = 'unmapped_dep_loc', 617 | format_error('rebar_api', 'error', Class, Loc), 618 | _ = dump_mod_data(Data), 619 | ?throw_error({Class, Loc}) 620 | end. 621 | 622 | -spec lookup_res(Data :: mod_data(), Res :: rsrc_type()) 623 | -> mod_res() | no_return(). 624 | lookup_res(#mod_data{rsrcs = Rsrcs} = Data, Res) -> 625 | case lists:keyfind(Res, #mod_res.res, Rsrcs) of 626 | #mod_res{} = Rec -> 627 | Rec; 628 | _ -> 629 | Class = 'unmapped_res_type', 630 | format_error('rebar_api', 'error', Class, Res), 631 | _ = dump_mod_data(Data), 632 | ?throw_error({Class, Res}) 633 | end. 634 | 635 | % 636 | % To debug, or not to debug. 637 | % 638 | -spec dump_mod_data(Data :: mod_data()) -> mod_data(). 639 | -compile({nowarn_unused_function, debug_call_stack/0}). 640 | -spec debug_call_stack() -> term(). 641 | 642 | -ifndef(RRR_DEBUG). 643 | 644 | -compile({inline, debug_call_stack/0}). 645 | debug_call_stack() -> 646 | 'ok'. 647 | 648 | -compile({inline, dump_mod_data/1}). 649 | dump_mod_data(Data) -> 650 | Data. 651 | 652 | -else. 653 | 654 | debug_call_stack() -> 655 | {_, {_, [_ | Stack]}} = (catch erlang:error('ok')), 656 | rebar_api:debug("~s stack:~n~p", [?MODULE, Stack]). 657 | 658 | dump_mod_data(#mod_data{} = Data) -> 659 | rebar_api:debug("~s state:~n~p", [?MODULE, Data]), 660 | Data. 661 | 662 | % If the ?RRR_STATE(Field) macro isn't used, these functions won't be called. 663 | % The compiler will leave them out, but we need to disable the unused function 664 | % warning to get through compilation. 665 | -compile({nowarn_unused_function, [ 666 | debug_state/2, 667 | state_file_path/1, 668 | state_file_path/2, 669 | write_state_file/2 670 | ]}). 671 | 672 | debug_state(State, 'state' = Field) -> 673 | write_state_file(state_file_path(Field), State); 674 | debug_state(State, Field) -> 675 | case erlang:is_atom(Field) andalso 676 | erlang:function_exported('rebar_state', Field, 1) of 677 | true -> 678 | write_state_file(state_file_path(Field), 679 | rebar_state:Field(State)); 680 | _ -> 681 | write_state_file(state_file_path(['opts', Field]), 682 | rebar_state:get(State, Field, '$not_found$')) 683 | end. 684 | 685 | state_file_path('state') -> 686 | state_file_path(['state'], []); 687 | state_file_path(Field) when erlang:is_list(Field) -> 688 | state_file_path(['state' | Field], []); 689 | state_file_path(Field) -> 690 | state_file_path(['state', Field], []). 691 | 692 | state_file_path([Scope | Scopes], Key) when erlang:is_tuple(Scope) -> 693 | state_file_path(erlang:tuple_to_list(Scope) ++ Scopes, Key); 694 | state_file_path([Scope | Scopes], []) -> 695 | state_file_path(Scopes, erlang:atom_to_list(Scope)); 696 | state_file_path([Scope | Scopes], Key) -> 697 | state_file_path(Scopes, Key ++ [$. | erlang:atom_to_list(Scope)]); 698 | state_file_path([], Key) -> 699 | io_lib:format("/tmp/rebar.~s.config", [Key]). 700 | 701 | write_state_file(File, Data) when ?is_rec_type(Data, 'dict') -> 702 | file:write_file(File, io_lib:format("~p.~n", [dict:to_list(Data)])); 703 | write_state_file(File, Data) -> 704 | file:write_file(File, io_lib:format("~p.~n", [Data])). 705 | 706 | -endif. 707 | --------------------------------------------------------------------------------