Compare commits
707
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
307675284c | ||
|
|
38a9513111 | ||
|
|
578f798cc1 | ||
|
|
efbdf4b9b0 | ||
|
|
9679bed4d7 | ||
|
|
b6c0045fc2 | ||
|
|
10612ddf22 | ||
|
|
cbcbbcf876 | ||
|
|
95b81c2a13 | ||
|
|
a525f71e13 | ||
|
|
750d2a3f7a | ||
|
|
61e9fafb73 | ||
|
|
aa48f6a881 | ||
|
|
b5e2d45b7d | ||
|
|
29f2eb7dab | ||
|
|
a3754e3841 | ||
|
|
337c040da8 | ||
|
|
b4d465e2ed | ||
|
|
0bb3d1c6bd | ||
|
|
86476615ef | ||
|
|
860b3cc5a7 | ||
|
|
bb5e6f65f5 | ||
|
|
95127516f7 | ||
|
|
ef0c495f16 | ||
|
|
2472bb79ab | ||
|
|
04b9174d75 | ||
|
|
d31a58f0d9 | ||
|
|
f97ab2649c | ||
|
|
d969d42784 | ||
|
|
17c4c4d993 | ||
|
|
0f7a968389 | ||
|
|
272ec8ca47 | ||
|
|
a0e8e1c0fb | ||
|
|
b477dba34f | ||
|
|
8d524ef971 | ||
|
|
83cb5152fd | ||
|
|
3c100575a3 | ||
|
|
d1b6161f2e | ||
|
|
785781d089 | ||
|
|
b2ac4c9394 | ||
|
|
50ac792747 | ||
|
|
7d433e80e5 | ||
|
|
f9cb812bc1 | ||
|
|
f8711f2e36 | ||
|
|
0251f564d9 | ||
|
|
e4acc08cf5 | ||
|
|
2ab5d25787 | ||
|
|
acde8078f8 | ||
|
|
487ac9c1c4 | ||
|
|
f0f65f8bec | ||
|
|
23943b6180 | ||
|
|
d9125b0a81 | ||
|
|
d1d46db907 | ||
|
|
02056d1030 | ||
|
|
14a3686a3f | ||
|
|
c04b182c6c | ||
|
|
f5462d98e0 | ||
|
|
75d0c07b46 | ||
|
|
1de8dcb695 | ||
|
|
0e167bef27 | ||
|
|
34df817638 | ||
|
|
311a5790d6 | ||
|
|
b4c75f97b4 | ||
|
|
70692e6618 | ||
|
|
529d584262 | ||
|
|
c0fb275d5b | ||
|
|
24f46be144 | ||
|
|
0fe3da9dea | ||
|
|
03d0ad3f03 | ||
|
|
a87f43701f | ||
|
|
dce3a3a2a5 | ||
|
|
7b20084b28 | ||
|
|
542808102f | ||
|
|
fd18b6a41f | ||
|
|
4c9c95f80f | ||
|
|
89460154bb | ||
|
|
b026e8877a | ||
|
|
597381b2e0 | ||
|
|
df70729b14 | ||
|
|
61b116c1a5 | ||
|
|
d37a10bbd0 | ||
|
|
5d75c5c861 | ||
|
|
db4089cc71 | ||
|
|
f9f4f14edf | ||
|
|
58946ce879 | ||
|
|
de1d5ff793 | ||
|
|
a4be8fb260 | ||
|
|
031a27f5fa | ||
|
|
f3c1931cac | ||
|
|
2e48d81c20 | ||
|
|
e7ef3fc95d | ||
|
|
2d01aa1c6a | ||
|
|
ae07124550 | ||
|
|
1f744f4dac | ||
|
|
ad88ac642f | ||
|
|
f7956e9d80 | ||
|
|
da6892aa96 | ||
|
|
7a3bd20804 | ||
|
|
f7015166dc | ||
|
|
c6f346a355 | ||
|
|
911a3ba97f | ||
|
|
a1668a6b28 | ||
|
|
a4cb74a83f | ||
|
|
bbf9552186 | ||
|
|
e621b5828e | ||
|
|
68e282c50a | ||
|
|
403a50fbcd | ||
|
|
8e98cd4f41 | ||
|
|
499c385000 | ||
|
|
aecafc099d | ||
|
|
785c7d700a | ||
|
|
cecc8a0f95 | ||
|
|
52ff7e9686 | ||
|
|
e2eb2cfdb1 | ||
|
|
a5b4c1c04d | ||
|
|
e38f960d7e | ||
|
|
9e74ee686e | ||
|
|
e068625d7d | ||
|
|
00e8446a58 | ||
|
|
2aecfede5b | ||
|
|
9eebefb976 | ||
|
|
b709cba371 | ||
|
|
1d919f59e3 | ||
|
|
0e8d3f9208 | ||
|
|
8931f34582 | ||
|
|
843706e36e | ||
|
|
172bfdb128 | ||
|
|
2c741ec6ae | ||
|
|
12a457829f | ||
|
|
0ff6850237 | ||
|
|
63f6088476 | ||
|
|
206f6413f8 | ||
|
|
a2c0424b37 | ||
|
|
6ea7d3cef6 | ||
|
|
2cafd8c060 | ||
|
|
7271dfebfb | ||
|
|
2eff3590c1 | ||
|
|
b578f45060 | ||
|
|
a34ccfe129 | ||
|
|
8f4442f2df | ||
|
|
598e42e00b | ||
|
|
b93912b9dc | ||
|
|
71eca8a5d6 | ||
|
|
f0bb0e1f7f | ||
|
|
2cb0639208 | ||
|
|
081e3b36ec | ||
|
|
d521d7927f | ||
|
|
7313d26720 | ||
|
|
577a07d47f | ||
|
|
f6093d40d7 | ||
|
|
1dd034d4aa | ||
|
|
2417b196a4 | ||
|
|
50bb712e22 | ||
|
|
7b0747c907 | ||
|
|
b878e414de | ||
|
|
c447868e0f | ||
|
|
681d5aa9c4 | ||
|
|
0263dcc621 | ||
|
|
1ae24c5c5d | ||
|
|
6c02ebe4af | ||
|
|
a08e44c5a9 | ||
|
|
08f30098db | ||
|
|
5dbd91e4fa | ||
|
|
5b2eb54794 | ||
|
|
2c17007693 | ||
|
|
2770c32f3d | ||
|
|
8c8f6d8346 | ||
|
|
3950130e73 | ||
|
|
c8bcd89c7c | ||
|
|
7f7aaa115f | ||
|
|
53aeb26407 | ||
|
|
bba0d12510 | ||
|
|
035d8227a5 | ||
|
|
003a4da87e | ||
|
|
1977631b7f | ||
|
|
c9a0ec000a | ||
|
|
ba842ba113 | ||
|
|
6304919d0d | ||
|
|
d2d27e96e5 | ||
|
|
fc6f7c532c | ||
|
|
1bee85e518 | ||
|
|
66b211987d | ||
|
|
15fdae4de6 | ||
|
|
5b4497fa6b | ||
|
|
612e124ec9 | ||
|
|
abd2b35738 | ||
|
|
2afb875099 | ||
|
|
88507f65f8 | ||
|
|
275d1f5494 | ||
|
|
e6420c8d13 | ||
|
|
60bcf1cadb | ||
|
|
d86ca5826c | ||
|
|
2af00f4c33 | ||
|
|
0fe86e77c1 | ||
|
|
1ca451f779 | ||
|
|
ebe0c8cf8e | ||
|
|
de9feb26d5 | ||
|
|
b8528f6c8e | ||
|
|
c9fbbac391 | ||
|
|
98e1b4ccf8 | ||
|
|
ae19491620 | ||
|
|
64f3760b19 | ||
|
|
0b38bc050f | ||
|
|
9431338654 | ||
|
|
88be750a32 | ||
|
|
032c8b8291 | ||
|
|
2d90d8ac60 | ||
|
|
a8b2cb99ee | ||
|
|
68274311fb | ||
|
|
88f84f80b5 | ||
|
|
2bf898e8fe | ||
|
|
d45f451943 | ||
|
|
c0a075724f | ||
|
|
f1a39375c9 | ||
|
|
b9c1b22163 | ||
|
|
be75e3be17 | ||
|
|
9793caa1ef | ||
|
|
0792fe2c52 | ||
|
|
4408aa3450 | ||
|
|
e4ba8d5d21 | ||
|
|
689f14a867 | ||
|
|
aa5b414e8e | ||
|
|
07467a733b | ||
|
|
74391b793c | ||
|
|
f0b8bc1756 | ||
|
|
3e54b95bf2 | ||
|
|
313d26cb3b | ||
|
|
44b85be014 | ||
|
|
e344b1f9d8 | ||
|
|
f5f4205969 | ||
|
|
538c717ded | ||
|
|
8dd81e661a | ||
|
|
26b61b98f9 | ||
|
|
ab9450d92f | ||
|
|
a4a23b9a93 | ||
|
|
2f83ea5a7f | ||
|
|
7940d223ec | ||
|
|
c6fa951520 | ||
|
|
e5c8194a70 | ||
|
|
4a7e3dd11f | ||
|
|
638fccb79d | ||
|
|
e9d84ec43b | ||
|
|
bb35b9afb1 | ||
|
|
1496c04592 | ||
|
|
3e9725a5a8 | ||
|
|
1565ec7658 | ||
|
|
f1aa2f01c7 | ||
|
|
7284542116 | ||
|
|
4f17e8fd1c | ||
|
|
f536507b9d | ||
|
|
592300a0f0 | ||
|
|
28b1bd16de | ||
|
|
19fdd84f80 | ||
|
|
631e2a051a | ||
|
|
df0f7b5f95 | ||
|
|
a0dfc4160e | ||
|
|
ee8331023e | ||
|
|
a2ee71f5ab | ||
|
|
60163194e2 | ||
|
|
ba37682300 | ||
|
|
15265b9352 | ||
|
|
0a929310de | ||
|
|
a8670c1cfa | ||
|
|
ebd18d45fa | ||
|
|
ccbfec611a | ||
|
|
98a0999c2e | ||
|
|
e504142d3a | ||
|
|
c04e13169f | ||
|
|
96a4bff67b | ||
|
|
46de5ea462 | ||
|
|
22257b8342 | ||
|
|
a0be9fe272 | ||
|
|
1fd5a2a38e | ||
|
|
13aec18bd5 | ||
|
|
a85938d092 | ||
|
|
0d4af57abe | ||
|
|
e00db53112 | ||
|
|
b941618a10 | ||
|
|
d895c762a1 | ||
|
|
427c4b01c9 | ||
|
|
9d5a728cb8 | ||
|
|
1ed99d4ac7 | ||
|
|
ceb17fac7d | ||
|
|
c638f45542 | ||
|
|
c09905d537 | ||
|
|
117a428030 | ||
|
|
2ada7c40d7 | ||
|
|
cd162bf3d6 | ||
|
|
e6e71675ac | ||
|
|
c54e0caf6f | ||
|
|
b64552780e | ||
|
|
add8c66e82 | ||
|
|
aecbd2c16a | ||
|
|
32a649c1cd | ||
|
|
a1e657cb75 | ||
|
|
6bb4c4de03 | ||
|
|
ea5b56d434 | ||
|
|
a4082748cf | ||
|
|
4d763846c3 | ||
|
|
d56cc88f5f | ||
|
|
5e27b7b72b | ||
|
|
260d6d16ab | ||
|
|
1a82f126a4 | ||
|
|
3cf3eee2ea | ||
|
|
9e8a41b2d3 | ||
|
|
a54d9212b8 | ||
|
|
ee33608f75 | ||
|
|
6e91275782 | ||
|
|
c6f27541c1 | ||
|
|
a2662a4b51 | ||
|
|
1d29262949 | ||
|
|
1d655fa8f2 | ||
|
|
4bec785f4c | ||
|
|
e3273f731f | ||
|
|
a8564a7045 | ||
|
|
e2bac56556 | ||
|
|
a38e6b7a0d | ||
|
|
72199ca71d | ||
|
|
9420945f59 | ||
|
|
4ca1736f86 | ||
|
|
fd85ca443e | ||
|
|
187e86749e | ||
|
|
e04a7aef6a | ||
|
|
0b1c8c92c0 | ||
|
|
bf270d144f | ||
|
|
2c009d1ec3 | ||
|
|
08555f545c | ||
|
|
65d3af694b | ||
|
|
8aa43eaedd | ||
|
|
334c732691 | ||
|
|
24587110ad | ||
|
|
b98421fe51 | ||
|
|
752e3f8bc3 | ||
|
|
6c8e70b1f8 | ||
|
|
c6fbfd2a67 | ||
|
|
56ea6e52e4 | ||
|
|
4726e0af91 | ||
|
|
ee82679908 | ||
|
|
aedd5a938f | ||
|
|
2d9c3fb6f0 | ||
|
|
6f07f4af22 | ||
|
|
656bd640e6 | ||
|
|
bc2cb15a96 | ||
|
|
dbf205b311 | ||
|
|
0e0bb1c2a7 | ||
|
|
736406a9b3 | ||
|
|
886917753e | ||
|
|
f29889da99 | ||
|
|
aee33eac6d | ||
|
|
d8428ba1e6 | ||
|
|
1f725b0d2c | ||
|
|
38f895cc13 | ||
|
|
3fc07eff8e | ||
|
|
aa244c30fc | ||
|
|
f8647dae3e | ||
|
|
39fa2878ce | ||
|
|
5b5b95eb11 | ||
|
|
67caa3ebb3 | ||
|
|
d09cd173bf | ||
|
|
79fb40ab6a | ||
|
|
ae2cb6bc43 | ||
|
|
413b815ecc | ||
|
|
df89b5022e | ||
|
|
90729655ee | ||
|
|
171e9b3659 | ||
|
|
3e256fcc1e | ||
|
|
29ebbeda93 | ||
|
|
214d056911 | ||
|
|
9f065a77ca | ||
|
|
e70ac9768e | ||
|
|
ec6c98ef47 | ||
|
|
7d0f92e12a | ||
|
|
8bf6193ccc | ||
|
|
50f5dc76fb | ||
|
|
b9000999c9 | ||
|
|
52be06c314 | ||
|
|
3da560f10c | ||
|
|
f0f25300a0 | ||
|
|
c8a3a619af | ||
|
|
25f64051ba | ||
|
|
5efa6bb1c4 | ||
|
|
cb48f90f28 | ||
|
|
3ba93b1186 | ||
|
|
7babcd0ed2 | ||
|
|
b6c1f2b2e1 | ||
|
|
5f853f8e71 | ||
|
|
6cccf3ac22 | ||
|
|
5b9b186d4b | ||
|
|
57898bee03 | ||
|
|
6a6ba9f50d | ||
|
|
1e3135514f | ||
|
|
2f2a52a648 | ||
|
|
89fd8f305f | ||
|
|
353c043d8c | ||
|
|
54a40efeec | ||
|
|
f781795032 | ||
|
|
54ffb5768d | ||
|
|
8fc068821f | ||
|
|
1f91fd351e | ||
|
|
ab4399323f | ||
|
|
3b34a57bd2 | ||
|
|
62a469dc23 | ||
|
|
8b0c4127ef | ||
|
|
dc4b7c89ea | ||
|
|
b5febd991c | ||
|
|
f1323ade7c | ||
|
|
89561c321d | ||
|
|
d1b8657933 | ||
|
|
89d9cc89ca | ||
|
|
7b0cbb1632 | ||
|
|
3dc0e87c33 | ||
|
|
a19613cf19 | ||
|
|
a8e4fcdb6c | ||
|
|
a70ca83ba2 | ||
|
|
11c857a9a2 | ||
|
|
84b1f6546b | ||
|
|
c8c1757fdd | ||
|
|
9a162b5076 | ||
|
|
92d2f25048 | ||
|
|
a2ddb6abbb | ||
|
|
ac629c875b | ||
|
|
ab8ba93d1d | ||
|
|
9d62e674d7 | ||
|
|
58ca3401e4 | ||
|
|
a5926616a6 | ||
|
|
59910ae4bf | ||
|
|
69b47483c1 | ||
|
|
284e75d031 | ||
|
|
c1d63e4639 | ||
|
|
212b204af8 | ||
|
|
90f83e1b3d | ||
|
|
562af52d07 | ||
|
|
ed79d60cef | ||
|
|
42d378142b | ||
|
|
aeefd59b15 | ||
|
|
6db5091378 | ||
|
|
4e7ba9d598 | ||
|
|
60b66ac669 | ||
|
|
1fda82d9c2 | ||
|
|
55dbd617a6 | ||
|
|
841a4e0195 | ||
|
|
5d60728d4b | ||
|
|
9717a63604 | ||
|
|
fae20670ed | ||
|
|
6874eb7ce8 | ||
|
|
404e95b7ac | ||
|
|
30a5914a92 | ||
|
|
f97a36ff98 | ||
|
|
e68f617e2a | ||
|
|
3056e95ded | ||
|
|
9ba77e1ce6 | ||
|
|
84010d4467 | ||
|
|
a91aa70103 | ||
|
|
301c7fd122 | ||
|
|
8809ab2f5e | ||
|
|
71e441dcc2 | ||
|
|
f11197fd04 | ||
|
|
7ac771da67 | ||
|
|
a47751f4de | ||
|
|
c91217860c | ||
|
|
f69e9df983 | ||
|
|
2ee1d5daef | ||
|
|
d3dba63659 | ||
|
|
c016d74379 | ||
|
|
a6d887ee09 | ||
|
|
aa017a4dcb | ||
|
|
b7c7d7e72c | ||
|
|
d164a9b93f | ||
|
|
0105cf538e | ||
|
|
957e389fe7 | ||
|
|
88d4d38afa | ||
|
|
8b4b985894 | ||
|
|
986eb8bdb1 | ||
|
|
052eac333c | ||
|
|
acae645db1 | ||
|
|
7cb5f6ff3c | ||
|
|
0fe940a621 | ||
|
|
81ed03ffb2 | ||
|
|
3947f7455a | ||
|
|
406a2e6755 | ||
|
|
0b3fcb254a | ||
|
|
054843505a | ||
|
|
92016cd80e | ||
|
|
0c06e72f0c | ||
|
|
a722a4b33c | ||
|
|
4afb8a516c | ||
|
|
e9fc50bc6d | ||
|
|
0a5a980ab7 | ||
|
|
30505b7758 | ||
|
|
9f867f2ec8 | ||
|
|
5e2020804a | ||
|
|
b5cea26e57 | ||
|
|
03e6d3a5a2 | ||
|
|
89513f9d15 | ||
|
|
f20ef46e90 | ||
|
|
d7ebc46a2d | ||
|
|
bd6d4bcd6f | ||
|
|
4f8862cedd | ||
|
|
1afc5ec71b | ||
|
|
925ff6050a | ||
|
|
6ce97090f3 | ||
|
|
6538f33825 | ||
|
|
a1e4a2741f | ||
|
|
fe57a821d8 | ||
|
|
c42fe88dcc | ||
|
|
28d7fc35a6 | ||
|
|
9fa279897b | ||
|
|
6465a3a4af | ||
|
|
1582df8d70 | ||
|
|
8d9aec5a55 | ||
|
|
7f4df95922 | ||
|
|
98a5affa4a | ||
|
|
b720559d56 | ||
|
|
cd5cc91cd4 | ||
|
|
93d92d1f36 | ||
|
|
ce3674dd07 | ||
|
|
e4cfb62a74 | ||
|
|
bb51701614 | ||
|
|
6ffb248f03 | ||
|
|
50af8058ef | ||
|
|
b1d9e147b3 | ||
|
|
7ea91d2d68 | ||
|
|
c7640d3cf4 | ||
|
|
399240295b | ||
|
|
b965d8eb5d | ||
|
|
380bf1e279 | ||
|
|
6272b60d36 | ||
|
|
0c7699f21c | ||
|
|
1086a8455f | ||
|
|
d8e7578e41 | ||
|
|
2d0fbcc23c | ||
|
|
21d5d43ac5 | ||
|
|
027f90380b | ||
|
|
1a68f63525 | ||
|
|
798c53a4cb | ||
|
|
e3a89050f2 | ||
|
|
f176cb55c1 | ||
|
|
97f2e8744b | ||
|
|
c513459c10 | ||
|
|
fd6f1d47be | ||
|
|
6b8b5bfd01 | ||
|
|
e6e96165aa | ||
|
|
42875c0b72 | ||
|
|
b35e2cf82a | ||
|
|
be320880b2 | ||
|
|
fe1712d642 | ||
|
|
63d815912e | ||
|
|
cd4dc75c93 | ||
|
|
058b157b36 | ||
|
|
19c9941e7f | ||
|
|
7895ca0821 | ||
|
|
2b20bb424f | ||
|
|
82535feb90 | ||
|
|
81f007bdd7 | ||
|
|
0b35996d5e | ||
|
|
fc5cf506ae | ||
|
|
c4582e0041 | ||
|
|
c80d215674 | ||
|
|
3ee9d5d0df | ||
|
|
d86ba0e1c7 | ||
|
|
c295ee50de | ||
|
|
54c25a55fe | ||
|
|
583a4ec5f6 | ||
|
|
69b78dad51 | ||
|
|
0685e0315a | ||
|
|
5e429001d9 | ||
|
|
707b810f1b | ||
|
|
e51cfc142d | ||
|
|
b259e793bb | ||
|
|
32738a97d9 | ||
|
|
45c6fbaced | ||
|
|
bba4f1bd1b | ||
|
|
ade2596e92 | ||
|
|
0e804a2b5c | ||
|
|
33ab1679f3 | ||
|
|
f2b12633f3 | ||
|
|
5a4b2769ee | ||
|
|
2439bae409 | ||
|
|
6ed7e40d03 | ||
|
|
2fc76fe1b0 | ||
|
|
e541a56130 | ||
|
|
6378750b12 | ||
|
|
fc25f60718 | ||
|
|
3b351e7650 | ||
|
|
999a535603 | ||
|
|
a726dd812b | ||
|
|
58d54afac6 | ||
|
|
001d776176 | ||
|
|
4bcd25fb4d | ||
|
|
f0fa9e66a6 | ||
|
|
12f9bf09b0 | ||
|
|
bf8f5a995f | ||
|
|
d3a3feace4 | ||
|
|
30331b2bea | ||
|
|
f5bf9d9659 | ||
|
|
2865bd8653 | ||
|
|
4513b6965f | ||
|
|
b47e8c61d7 | ||
|
|
40bf079566 | ||
|
|
2eaf854e79 | ||
|
|
7c12cf029c | ||
|
|
597d90b326 | ||
|
|
5dcad26b79 | ||
|
|
f8c2d5cb4f | ||
|
|
b0f432b2f8 | ||
|
|
651a4bad67 | ||
|
|
f52e350adc | ||
|
|
35e8019447 | ||
|
|
a6fab4a111 | ||
|
|
64d62d2472 | ||
|
|
10b322aa48 | ||
|
|
981955301d | ||
|
|
ac425f04ef | ||
|
|
1dda1563da | ||
|
|
0e71b1e711 | ||
|
|
841f6f2387 | ||
|
|
2eb7fb97de | ||
|
|
e781983e6b | ||
|
|
7d17a12783 | ||
|
|
d888057689 | ||
|
|
76d6e05399 | ||
|
|
1bd0ca6dea | ||
|
|
8e1f7fddca | ||
|
|
abda47903b | ||
|
|
030e72c9c6 | ||
|
|
88dbad9650 | ||
|
|
07263b8533 | ||
|
|
f67741b6dc | ||
|
|
4724ff12e2 | ||
|
|
f7c7ff46b4 | ||
|
|
c4868f8549 | ||
|
|
85951052ca | ||
|
|
28bd15b76c | ||
|
|
1fdb715967 | ||
|
|
0279547232 | ||
|
|
7fabf32d9e | ||
|
|
4cbf0eb2ac | ||
|
|
071b0830f8 | ||
|
|
39cd4a6594 | ||
|
|
0d2332fe5e | ||
|
|
c807fa6ec5 | ||
|
|
4a10806d9a | ||
|
|
57031f8cb0 | ||
|
|
b292486ac9 | ||
|
|
595f9bb0b1 | ||
|
|
ebe3397ad9 | ||
|
|
bc12b76bcc | ||
|
|
d8df85e99c | ||
|
|
80b9879d0a | ||
|
|
d54f9ec931 | ||
|
|
d2258acbb3 | ||
|
|
6c3a4bd019 | ||
|
|
7903211546 | ||
|
|
6b87fcf903 | ||
|
|
07d0d1b275 | ||
|
|
95361172a0 | ||
|
|
a0f3086167 | ||
|
|
65a59b41e5 | ||
|
|
78dc02fb81 | ||
|
|
dc577b8bfa | ||
|
|
ed9862ea91 | ||
|
|
f710c61179 | ||
|
|
d86cf14f0d | ||
|
|
bdfd5817dc | ||
|
|
3b2ece4ddc | ||
|
|
3452f364aa | ||
|
|
dad77a4d4b | ||
|
|
c8b925c581 | ||
|
|
24297c87e6 | ||
|
|
0c589eff04 | ||
|
|
3a3684ca78 | ||
|
|
6e39366c40 | ||
|
|
fd0ccb7741 | ||
|
|
81f151245e | ||
|
|
acfdd5b59e | ||
|
|
588800c495 | ||
|
|
cfd157ab7d | ||
|
|
377eb6126d | ||
|
|
2cad4c96bd | ||
|
|
2453501c83 | ||
|
|
dcc3c6290e | ||
|
|
54240e1b4f | ||
|
|
6639f91ba2 | ||
|
|
15bf309760 | ||
|
|
1de0302ecb | ||
|
|
871cbda3f8 | ||
|
|
c4ea85d7e5 | ||
|
|
a8f3125774 | ||
|
|
c564af180b | ||
|
|
3ebe1ed728 | ||
|
|
6b030b2978 | ||
|
|
9f2fa0f7af | ||
|
|
da06e35443 | ||
|
|
da4d047a37 | ||
|
|
782314abff | ||
|
|
94d53f052b | ||
|
|
9ceebf96db | ||
|
|
8611bb7322 | ||
|
|
ba2e1c95ef | ||
|
|
5979b5ca75 | ||
|
|
f144cae300 | ||
|
|
8bcf473b68 | ||
|
|
b10fdfc7b8 | ||
|
|
95eb1c0978 | ||
|
|
7a5cb8d374 | ||
|
|
f23b133d29 | ||
|
|
0bdee49f7f |
@@ -1,22 +0,0 @@
|
||||
[
|
||||
inputs: [
|
||||
"lib/*/{lib,scripts,unicode,test}/**/*.{ex,exs}",
|
||||
"lib/*/*.exs",
|
||||
"lib/ex_unit/examples/*.exs",
|
||||
".formatter.exs"
|
||||
],
|
||||
locals_without_parens: [
|
||||
# Formatter tests
|
||||
assert_format: 2,
|
||||
assert_format: 3,
|
||||
assert_same: 1,
|
||||
assert_same: 2,
|
||||
|
||||
# Errors tests
|
||||
assert_eval_raise: 3,
|
||||
|
||||
# Float tests
|
||||
float_assert: 1
|
||||
],
|
||||
normalize_bitstring_modifiers: false
|
||||
]
|
||||
@@ -1,3 +1 @@
|
||||
lib/elixir/test/elixir/fixtures/*.txt text eol=lf
|
||||
*.ex diff=elixir
|
||||
*.exs diff=elixir
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
blank_issues_enabled: true
|
||||
|
||||
contact_links:
|
||||
- name: Ask questions, support, and general discussions
|
||||
url: https://elixirforum.com/
|
||||
about: Ask questions, provide support, and more on Elixir Forum
|
||||
|
||||
- name: Propose new features
|
||||
url: https://github.com/elixir-lang/elixir/#proposing-new-features
|
||||
about: Propose new features in our mailing list
|
||||
@@ -1,53 +0,0 @@
|
||||
---
|
||||
name: Report an issue
|
||||
description:
|
||||
Tell us about something that is not working the way we (probably) intend
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: >
|
||||
Thank you for contributing to Elixir! :heart:
|
||||
|
||||
|
||||
Please, do not use this form for guidance, questions or support.
|
||||
Try instead in [Elixir Forum](https://elixirforum.com),
|
||||
the [IRC Chat](https://web.libera.chat/#elixir),
|
||||
[Stack Overflow](https://stackoverflow.com/questions/tagged/elixir),
|
||||
[Slack](https://elixir-slackin.herokuapp.com),
|
||||
[Discord](https://discord.gg/elixir) or in other online communities.
|
||||
|
||||
- type: textarea
|
||||
id: elixir-and-otp-version
|
||||
attributes:
|
||||
label: Elixir and Erlang/OTP versions
|
||||
description: Paste the output of `elixir --version` here.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
id: os
|
||||
attributes:
|
||||
label: Operating system
|
||||
description: The operating system that this issue is happening on.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: current-behavior
|
||||
attributes:
|
||||
label: Current behavior
|
||||
description: >
|
||||
Include code samples, errors, and stacktraces if appropriate.
|
||||
|
||||
|
||||
If reporting a bug, please include the reproducing steps.
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: expected-behavior
|
||||
attributes:
|
||||
label: Expected behavior
|
||||
description: A short description on how you expect the code to behave.
|
||||
validations:
|
||||
required: true
|
||||
@@ -1,6 +0,0 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
@@ -1,134 +0,0 @@
|
||||
name: builds.hex.pm
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- v*.*
|
||||
tags:
|
||||
- v*
|
||||
|
||||
env:
|
||||
ELIXIR_OPTS: "--warnings-as-errors"
|
||||
ERLC_OPTS: "warnings_as_errors"
|
||||
LANG: C.UTF-8
|
||||
|
||||
concurrency: builds_txt
|
||||
|
||||
jobs:
|
||||
release_pre_built:
|
||||
strategy:
|
||||
fail-fast: true
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
include:
|
||||
- otp: 25
|
||||
otp_version: "25.3"
|
||||
upload_generic_version: upload_generic_version
|
||||
- otp: 26
|
||||
otp_version: "26.0"
|
||||
- otp: 27
|
||||
otp_version: "27.0"
|
||||
build_docs: build_docs
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- name: Get tags
|
||||
run: git fetch --tags origin
|
||||
- uses: ./.github/workflows/release_pre_built
|
||||
with:
|
||||
otp_version: ${{ matrix.otp_version }}
|
||||
otp: ${{ matrix.otp }}
|
||||
build_docs: ${{ matrix.build_docs }}
|
||||
- name: Utils.sh
|
||||
run: |
|
||||
cat << 'EOF' > utils.sh
|
||||
function purge_key() {
|
||||
curl \
|
||||
-X POST \
|
||||
-H "Fastly-Key: ${FASTLY_KEY}" \
|
||||
-H "Accept: application/json" \
|
||||
-H "Content-Length: 0" \
|
||||
"https://api.fastly.com/service/$1/purge/$2"
|
||||
}
|
||||
function purge() {
|
||||
purge_key ${FASTLY_REPO_SERVICE_ID} $1
|
||||
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
|
||||
sleep 2
|
||||
purge_key ${FASTLY_REPO_SERVICE_ID} $1
|
||||
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
|
||||
sleep 2
|
||||
purge_key ${FASTLY_REPO_SERVICE_ID} $1
|
||||
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
|
||||
}
|
||||
EOF
|
||||
chmod +x utils.sh
|
||||
- name: Upload Docs to S3
|
||||
if: ${{ matrix.build_docs }}
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.HEX_AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.HEX_AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_REGION: ${{ secrets.HEX_AWS_REGION }}
|
||||
AWS_S3_BUCKET: ${{ secrets.HEX_AWS_S3_BUCKET }}
|
||||
FASTLY_REPO_SERVICE_ID: ${{ secrets.HEX_FASTLY_REPO_SERVICE_ID }}
|
||||
FASTLY_BUILDS_SERVICE_ID: ${{ secrets.HEX_FASTLY_BUILDS_SERVICE_ID }}
|
||||
FASTLY_KEY: ${{ secrets.HEX_FASTLY_KEY }}
|
||||
run: |
|
||||
source utils.sh
|
||||
version=$(echo ${{ github.ref_name }} | sed -e 's/^v//g')
|
||||
for f in doc/*; do
|
||||
if [ -d "$f" ]; then
|
||||
app=`echo $f | sed s/"doc\/"//`
|
||||
tarball="${app}-${version}.tar.gz"
|
||||
surrogate_key="docs/${app}-${version}"
|
||||
tar -czf "${tarball}" -C "doc/${app}" .
|
||||
aws s3 cp "${tarball}" "s3://${{ env.AWS_S3_BUCKET }}/docs/${tarball}" \
|
||||
--cache-control "public,max-age=3600" \
|
||||
--metadata "{\"surrogate-key\":\"${surrogate_key}\",\"surrogate-control\":\"public,max-age=604800\"}"
|
||||
purge "${surrogate_key}"
|
||||
fi
|
||||
done
|
||||
- name: Upload Precompiled to S3
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.HEX_AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.HEX_AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_REGION: ${{ secrets.HEX_AWS_REGION }}
|
||||
AWS_S3_BUCKET: ${{ secrets.HEX_AWS_S3_BUCKET }}
|
||||
FASTLY_REPO_SERVICE_ID: ${{ secrets.HEX_FASTLY_REPO_SERVICE_ID }}
|
||||
FASTLY_BUILDS_SERVICE_ID: ${{ secrets.HEX_FASTLY_BUILDS_SERVICE_ID }}
|
||||
FASTLY_KEY: ${{ secrets.HEX_FASTLY_KEY }}
|
||||
run: |
|
||||
source utils.sh
|
||||
aws s3 cp elixir-otp-${{ matrix.otp }}.zip "s3://${{ env.AWS_S3_BUCKET }}/builds/elixir/${{github.ref_name}}-otp-${{matrix.otp}}.zip" --cache-control "public,max-age=3600" --metadata "{\"surrogate-key\":\"builds builds/elixir builds/elixir/${{github.ref_name}}-otp-${{matrix.otp}}\",\"surrogate-control\":\"public,max-age=604800\"}"
|
||||
purge builds/elixir/${{github.ref_name}}-otp-${{matrix.otp}}
|
||||
if [ "${{matrix.upload_generic_version}}" == "upload_generic_version" ]; then
|
||||
aws s3 cp elixir-otp-${{ matrix.otp }}.zip "s3://${{ env.AWS_S3_BUCKET }}/builds/elixir/${{github.ref_name}}.zip" --cache-control "public,max-age=3600" --metadata "{\"surrogate-key\":\"builds builds/elixir builds/elixir/${{github.ref_name}}\",\"surrogate-control\":\"public,max-age=604800\"}"
|
||||
purge builds/elixir/${{github.ref_name}}
|
||||
fi
|
||||
- name: Update builds txt
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.HEX_AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.HEX_AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_REGION: ${{ secrets.HEX_AWS_REGION }}
|
||||
AWS_S3_BUCKET: ${{ secrets.HEX_AWS_S3_BUCKET }}
|
||||
FASTLY_REPO_SERVICE_ID: ${{ secrets.HEX_FASTLY_REPO_SERVICE_ID }}
|
||||
FASTLY_BUILDS_SERVICE_ID: ${{ secrets.HEX_FASTLY_BUILDS_SERVICE_ID }}
|
||||
FASTLY_KEY: ${{ secrets.HEX_FASTLY_KEY }}
|
||||
run: |
|
||||
source utils.sh
|
||||
date=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
|
||||
build_sha256=$(sha256sum elixir-otp-${{ matrix.otp }}.zip | cut -d ' ' -f 1)
|
||||
ref_name=${{ github.ref_name }}
|
||||
aws s3 cp s3://${{ env.AWS_S3_BUCKET }}/builds/elixir/builds.txt builds.txt || true
|
||||
touch builds.txt
|
||||
sed -i "/^${ref_name}-otp-${{ matrix.otp }} /d" builds.txt
|
||||
echo -e "${ref_name}-otp-${{ matrix.otp }} ${{ github.sha }} ${date} ${build_sha256} \n$(cat builds.txt)" > builds.txt
|
||||
if [ "${{matrix.upload_generic_version}}" == "upload_generic_version" ]; then
|
||||
sed -i "/^${ref_name} /d" builds.txt
|
||||
echo -e "${ref_name} ${{ github.sha }} ${date} ${build_sha256} \n$(cat builds.txt)" > builds.txt
|
||||
fi
|
||||
sort -u -k1,1 -o builds.txt builds.txt
|
||||
aws s3 cp builds.txt s3://${{ env.AWS_S3_BUCKET }}/builds/elixir/builds.txt --cache-control "public,max-age=3600" --metadata '{"surrogate-key":"builds builds/elixir builds/elixir/txt","surrogate-control":"public,max-age=604800"}'
|
||||
purge builds/elixir/txt
|
||||
@@ -1,34 +0,0 @@
|
||||
name: CI for Markdown content
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'main'
|
||||
paths:
|
||||
- 'lib/**/*.md'
|
||||
pull_request:
|
||||
paths:
|
||||
- 'lib/**/*.md'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: Lint Markdown content
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
steps:
|
||||
- name: Check out the repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 10
|
||||
|
||||
- name: Run markdownlint
|
||||
uses: DavidAnson/markdownlint-cli2-action@v16.0.0
|
||||
with:
|
||||
globs: |
|
||||
lib/elixir/pages/**/*.md
|
||||
README.md
|
||||
@@ -1,120 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
paths-ignore:
|
||||
- "lib/**/*.md"
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- "lib/**/*.md"
|
||||
|
||||
env:
|
||||
ELIXIR_ASSERT_TIMEOUT: 2000
|
||||
ELIXIRC_OPTS: "--warnings-as-errors"
|
||||
ERLC_OPTS: "warnings_as_errors"
|
||||
LANG: C.UTF-8
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
test_linux:
|
||||
name: Ubuntu 20.04, Erlang/OTP ${{ matrix.otp_version }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- otp_version: "27.0"
|
||||
otp_latest: true
|
||||
- otp_version: "26.0"
|
||||
- otp_version: "25.3"
|
||||
- otp_version: "25.0"
|
||||
- otp_version: master
|
||||
development: true
|
||||
- otp_version: maint
|
||||
development: true
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- uses: erlef/setup-beam@v1
|
||||
with:
|
||||
otp-version: ${{ matrix.otp_version }}
|
||||
- name: Compile Elixir
|
||||
run: |
|
||||
make compile
|
||||
echo "$PWD/bin" >> $GITHUB_PATH
|
||||
- name: Build info
|
||||
run: bin/elixir --version
|
||||
- name: Check format
|
||||
run: make test_formatted && echo "All Elixir source code files are properly formatted."
|
||||
- name: Erlang test suite
|
||||
run: make test_erlang
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
- name: Elixir test suite
|
||||
run: make test_elixir
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
- name: Build docs (ExDoc main)
|
||||
if: ${{ matrix.otp_latest }}
|
||||
run: |
|
||||
cd ..
|
||||
git clone https://github.com/elixir-lang/ex_doc.git --depth 1
|
||||
cd ex_doc
|
||||
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
|
||||
cd ../elixir/
|
||||
make docs
|
||||
- name: Check reproducible builds
|
||||
run: |
|
||||
rm -rf .git
|
||||
# Recompile System without .git
|
||||
cd lib/elixir && ../../bin/elixirc -o ebin lib/system.ex && cd -
|
||||
taskset 1 make check_reproducible
|
||||
if: ${{ matrix.otp_latest }}
|
||||
|
||||
test_windows:
|
||||
name: Windows Server 2019, Erlang/OTP ${{ matrix.otp_version }}
|
||||
strategy:
|
||||
matrix:
|
||||
otp_version: ["25.3", "26.0"]
|
||||
runs-on: windows-2019
|
||||
steps:
|
||||
- name: Configure Git
|
||||
run: git config --global core.autocrlf input
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- uses: erlef/setup-beam@v1
|
||||
with:
|
||||
otp-version: ${{ matrix.otp_version }}
|
||||
- name: Compile Elixir
|
||||
run: |
|
||||
Remove-Item -Recurse -Force '.git'
|
||||
make compile
|
||||
- name: Build info
|
||||
run: bin/elixir --version
|
||||
- name: Check format
|
||||
run: make test_formatted && echo "All Elixir source code files are properly formatted."
|
||||
- name: Erlang test suite
|
||||
run: make --keep-going test_erlang
|
||||
- name: Elixir test suite
|
||||
run: |
|
||||
Remove-Item 'c:/Windows/System32/drivers/etc/hosts'
|
||||
make --keep-going test_elixir
|
||||
|
||||
check_posix_compliant:
|
||||
name: Check POSIX-compliant
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- name: Install Shellcheck
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install -y shellcheck
|
||||
- name: Check POSIX-compliant
|
||||
run: |
|
||||
shellcheck -e SC2039,2086 bin/elixir && echo "bin/elixir is POSIX compliant"
|
||||
shellcheck bin/elixirc && echo "bin/elixirc is POSIX compliant"
|
||||
shellcheck bin/iex && echo "bin/iex is POSIX compliant"
|
||||
@@ -1,75 +0,0 @@
|
||||
# #!/usr/bin/env elixir
|
||||
[tag] = System.argv()
|
||||
|
||||
Mix.install([
|
||||
{:req, "~> 0.2.1"},
|
||||
{:jason, "~> 1.0"}
|
||||
])
|
||||
|
||||
%{status: 200, body: release} =
|
||||
Req.get!("https://api.github.com/repos/elixir-lang/elixir/releases/tags/#{tag}")
|
||||
|
||||
if release["draft"] do
|
||||
raise "cannot notify a draft release"
|
||||
end
|
||||
|
||||
## Notify on elixir-lang-ann
|
||||
|
||||
names_and_checksums =
|
||||
for asset <- release["assets"],
|
||||
name = asset["name"],
|
||||
name =~ ~r/.sha\d+sum$/,
|
||||
do: {name, Req.get!(asset["browser_download_url"]).body}
|
||||
|
||||
line_items =
|
||||
for {name, checksum_and_name} <- Enum.sort(names_and_checksums) do
|
||||
[checksum | _] = String.split(checksum_and_name, " ")
|
||||
root = Path.rootname(name)
|
||||
"." <> type = Path.extname(name)
|
||||
" * #{root} - #{type} - #{checksum}\n"
|
||||
end
|
||||
|
||||
body = "https://github.com/elixir-lang/elixir/releases/tag/#{tag}\n\n#{line_items}"
|
||||
|
||||
IO.puts([
|
||||
"========================================\n",
|
||||
body,
|
||||
"\n========================================"
|
||||
])
|
||||
|
||||
mail = %{
|
||||
# The email must have access to post
|
||||
"From" => "jose.valim@dashbit.co",
|
||||
"To" => "elixir-lang-ann@googlegroups.com",
|
||||
"Subject" => "Elixir #{tag} released",
|
||||
"HtmlBody" => body,
|
||||
"MessageStream" => "outbound"
|
||||
}
|
||||
|
||||
unless System.get_env("DRYRUN") do
|
||||
headers = %{
|
||||
"X-Postmark-Server-Token" => System.fetch_env!("ELIXIR_LANG_ANN_TOKEN")
|
||||
}
|
||||
|
||||
resp = Req.post!("https://api.postmarkapp.com/email", {:json, mail}, headers: headers)
|
||||
IO.puts("#{resp.status} elixir-lang-ann\n#{inspect(resp.body)}")
|
||||
end
|
||||
|
||||
## Notify on Elixir Forum
|
||||
|
||||
post = %{
|
||||
"title" => "Elixir #{tag} released",
|
||||
"raw" => "https://github.com/elixir-lang/elixir/releases/tag/#{tag}\n\n#{release["body"]}",
|
||||
# Elixir News
|
||||
"category" => 28
|
||||
}
|
||||
|
||||
unless System.get_env("DRYRUN") do
|
||||
headers = %{
|
||||
"api-key" => System.fetch_env!("ELIXIR_FORUM_TOKEN"),
|
||||
"api-username" => "Elixir"
|
||||
}
|
||||
|
||||
resp = Req.post!("https://elixirforum.com/posts.json", {:json, post}, headers: headers)
|
||||
IO.puts("#{resp.status} Elixir Forum\n#{inspect(resp.body)}")
|
||||
end
|
||||
@@ -1,28 +0,0 @@
|
||||
name: Notify
|
||||
|
||||
on:
|
||||
release:
|
||||
types:
|
||||
- published
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
notify:
|
||||
runs-on: ubuntu-20.04
|
||||
name: Notify
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- uses: erlef/setup-beam@v1
|
||||
with:
|
||||
otp-version: '25.0'
|
||||
elixir-version: '1.14.0'
|
||||
- name: Run Elixir script
|
||||
env:
|
||||
ELIXIR_FORUM_TOKEN: ${{ secrets.ELIXIR_FORUM_TOKEN }}
|
||||
ELIXIR_LANG_ANN_TOKEN: ${{ secrets.ELIXIR_LANG_ANN_TOKEN }}
|
||||
run: |
|
||||
elixir .github/workflows/notify.exs ${{ github.ref_name }}
|
||||
@@ -1,69 +0,0 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- v*
|
||||
|
||||
env:
|
||||
ELIXIR_OPTS: "--warnings-as-errors"
|
||||
ERLC_OPTS: "warnings_as_errors"
|
||||
LANG: C.UTF-8
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
create_draft_release:
|
||||
runs-on: ubuntu-22.04
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
steps:
|
||||
- name: Create draft release
|
||||
run: |
|
||||
gh release create \
|
||||
--repo ${{ github.repository }} \
|
||||
--title ${{ github.ref_name }} \
|
||||
--notes '' \
|
||||
--draft \
|
||||
${{ github.ref_name }}
|
||||
release_pre_built:
|
||||
needs: create_draft_release
|
||||
strategy:
|
||||
fail-fast: true
|
||||
matrix:
|
||||
include:
|
||||
- otp: 25
|
||||
otp_version: "25.3"
|
||||
- otp: 26
|
||||
otp_version: "26.0"
|
||||
- otp: 27
|
||||
otp_version: "27.0"
|
||||
build_docs: build_docs
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- uses: ./.github/workflows/release_pre_built
|
||||
with:
|
||||
otp_version: ${{ matrix.otp_version }}
|
||||
otp: ${{ matrix.otp }}
|
||||
build_docs: ${{ matrix.build_docs }}
|
||||
- name: Upload Pre-built
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
gh release upload --clobber "${{ github.ref_name }}" \
|
||||
elixir-otp-${{ matrix.otp }}.zip \
|
||||
elixir-otp-${{ matrix.otp }}.zip.sha{1,256}sum \
|
||||
elixir-otp-${{ matrix.otp }}.exe \
|
||||
elixir-otp-${{ matrix.otp }}.exe.sha{1,256}sum
|
||||
- name: Upload Docs to GitHub
|
||||
if: ${{ matrix.build_docs }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
gh release upload --clobber "${{ github.ref_name }}" \
|
||||
Docs.zip \
|
||||
Docs.zip.sha{1,256}sum
|
||||
@@ -1,69 +0,0 @@
|
||||
name: "Release pre built"
|
||||
description: "Builds elixir release, ExDoc and generates docs"
|
||||
inputs:
|
||||
otp:
|
||||
description: "The major OTP version"
|
||||
otp_version:
|
||||
description: "The exact OTP version (major.minor[.patch])"
|
||||
build_docs:
|
||||
description: "If docs have to be built or not"
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- uses: erlef/setup-beam@v1
|
||||
with:
|
||||
otp-version: ${{ inputs.otp_version }}
|
||||
version-type: strict
|
||||
- name: Build Elixir Release
|
||||
shell: bash
|
||||
run: |
|
||||
make Precompiled.zip
|
||||
mv Precompiled.zip elixir-otp-${{ inputs.otp }}.zip
|
||||
shasum -a 1 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha1sum
|
||||
shasum -a 256 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha256sum
|
||||
echo "$PWD/bin" >> $GITHUB_PATH
|
||||
- name: Install NSIS
|
||||
shell: bash
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install -y nsis
|
||||
- name: Build Elixir Windows Installer
|
||||
shell: bash
|
||||
run: |
|
||||
export OTP_VERSION=${{ inputs.otp_version }}
|
||||
export ELIXIR_ZIP=$PWD/elixir-otp-${{ inputs.otp }}.zip
|
||||
(cd lib/elixir/scripts/windows_installer && ./build.sh)
|
||||
mv lib/elixir/scripts/windows_installer/tmp/elixir-otp-${{ inputs.otp }}.exe .
|
||||
shasum -a 1 elixir-otp-${{ inputs.otp }}.exe > elixir-otp-${{ inputs.otp }}.exe.sha1sum
|
||||
shasum -a 256 elixir-otp-${{ inputs.otp }}.exe > elixir-otp-${{ inputs.otp }}.exe.sha256sum
|
||||
- name: Get ExDoc ref
|
||||
if: ${{ inputs.build_docs }}
|
||||
shell: bash
|
||||
run: |
|
||||
if [ "${{ github.ref_name }}" = "main" ]; then
|
||||
ref=main
|
||||
else
|
||||
ref=v$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
|
||||
fi
|
||||
echo "EX_DOC_REF=$ref" >> $GITHUB_ENV
|
||||
- uses: actions/checkout@v3
|
||||
if: ${{ inputs.build_docs }}
|
||||
with:
|
||||
repository: elixir-lang/ex_doc
|
||||
ref: ${{ env.EX_DOC_REF }}
|
||||
path: ex_doc
|
||||
- name: Build ex_doc
|
||||
if: ${{ inputs.build_docs }}
|
||||
shell: bash
|
||||
run: |
|
||||
mv ex_doc ../ex_doc
|
||||
cd ../ex_doc
|
||||
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
|
||||
cd ../elixir
|
||||
- name: Build Docs
|
||||
if: ${{ inputs.build_docs }}
|
||||
shell: bash
|
||||
run: |
|
||||
make Docs.zip
|
||||
shasum -a 1 Docs.zip > Docs.zip.sha1sum
|
||||
shasum -a 256 Docs.zip > Docs.zip.sha256sum
|
||||
+11
-11
@@ -1,13 +1,13 @@
|
||||
/doc/
|
||||
/lib/*/ebin/
|
||||
/lib/*/_build/
|
||||
/lib/*/tmp/
|
||||
/lib/elixir/src/*_parser.erl
|
||||
/lib/elixir/test/ebin/
|
||||
/man/elixir.1
|
||||
/man/iex.1
|
||||
/Docs-v*.zip
|
||||
/Precompiled-v*.zip
|
||||
/.eunit
|
||||
.elixir.plt
|
||||
/.release
|
||||
/doc
|
||||
/ebin
|
||||
/lib/*/ebin/*
|
||||
/lib/*/tmp
|
||||
/lib/elixir/src/elixir.app.src
|
||||
/lib/elixir/src/*_lexer.erl
|
||||
/lib/elixir/src/*_parser.erl
|
||||
/lib/elixir/test/ebin
|
||||
/rel/elixir
|
||||
erl_crash.dump
|
||||
.elixir.plt
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
{
|
||||
// Consecutive header levels (h1 -> h2 -> h3). We don't care about this.
|
||||
"MD001": false,
|
||||
// Header style. We use #s.
|
||||
"MD003": {
|
||||
"style": "atx"
|
||||
},
|
||||
// Style of unordered lists..
|
||||
"MD007": {
|
||||
"indent": 2,
|
||||
"start_indented": true
|
||||
},
|
||||
// Line length. Who cares.
|
||||
"MD013": false,
|
||||
// This warns if you have "console" or "shell" code blocks with a dollar sign $ that
|
||||
// don't show output. We use those a lot, so this is fine for us.
|
||||
"MD014": false,
|
||||
// Multiple headings with the same content. That's fine.
|
||||
"MD024": false,
|
||||
// Some headers finish with ! because it refers to a function name
|
||||
"MD026": false,
|
||||
// Allow empty line between block quotes. Used by contiguous admonition blocks.
|
||||
"MD028": false,
|
||||
// Allowed HTML inline elements.
|
||||
"MD033": {
|
||||
"allowed_elements": [
|
||||
"h1",
|
||||
"a",
|
||||
"br",
|
||||
"img",
|
||||
"picture",
|
||||
"source",
|
||||
"noscript",
|
||||
"p",
|
||||
"script"
|
||||
]
|
||||
},
|
||||
// This warns if you have spaces in code blocks. Sometimes, that's fine.
|
||||
"MD038": false,
|
||||
// Code block style. We don't care if it's fenced or indented.
|
||||
"MD046": false
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
language: erlang
|
||||
script: "make compile && rm -rf .git && make test"
|
||||
notifications:
|
||||
irc: "irc.freenode.org#elixir-lang"
|
||||
recipients:
|
||||
- jose.valim@plataformatec.com.br
|
||||
- eric.meadows.jonsson@gmail.com
|
||||
otp_release:
|
||||
- 17.0
|
||||
- 17.1
|
||||
- 17.3
|
||||
+1379
-197
File diff suppressed because it is too large
Load Diff
@@ -1,66 +0,0 @@
|
||||
# Code of Conduct
|
||||
|
||||
Contact: elixir-lang-conduct@googlegroups.com
|
||||
|
||||
## Why have a Code of Conduct?
|
||||
|
||||
As contributors and maintainers of this project, we are committed to providing a friendly, safe and welcoming environment for all, regardless of age, disability, gender, nationality, race, religion, sexuality, or similar personal characteristic.
|
||||
|
||||
The goal of the Code of Conduct is to specify a baseline standard of behavior so that people with different social values and communication styles can talk about Elixir effectively, productively, and respectfully, even in face of disagreements. The Code of Conduct also provides a mechanism for resolving conflicts in the community when they arise.
|
||||
|
||||
## Our Values
|
||||
|
||||
These are the values Elixir developers should aspire to:
|
||||
|
||||
* Be friendly and welcoming
|
||||
* Be kind
|
||||
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
|
||||
* Interpret the arguments of others in good faith, do not seek to disagree.
|
||||
* When we do disagree, try to understand why.
|
||||
* Be thoughtful
|
||||
* Productive communication requires effort. Think about how your words will be interpreted.
|
||||
* Remember that sometimes it is best to refrain entirely from commenting.
|
||||
* Be respectful
|
||||
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
|
||||
* Be constructive
|
||||
* Avoid derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Avoid unconstructive criticism: don't merely decry the current state of affairs; offer — or at least solicit — suggestions as to how things may be improved.
|
||||
* Avoid harsh words and stern tone: we are all aligned towards the well-being of the community and the progress of the ecosystem. Harsh words exclude, demotivate, and lead to unnecessary conflict.
|
||||
* Avoid snarking (pithy, unproductive, sniping comments).
|
||||
* Avoid microaggressions (brief and commonplace verbal, behavioral and environmental indignities that communicate hostile, derogatory or negative slights and insults towards a project, person or group).
|
||||
* Be responsible
|
||||
* What you say and do matters. Take responsibility for your words and actions, including their consequences, whether intended or otherwise.
|
||||
|
||||
The following actions are explicitly forbidden:
|
||||
|
||||
* Insulting, demeaning, hateful, or threatening remarks.
|
||||
* Discrimination based on age, disability, gender, nationality, race, religion, sexuality, or similar personal characteristic.
|
||||
* Bullying or systematic harassment.
|
||||
* Unwelcome sexual advances.
|
||||
* Incitement to any of these.
|
||||
|
||||
## Where does the Code of Conduct apply?
|
||||
|
||||
If you participate in or contribute to the Elixir ecosystem in any way, you are encouraged to follow the Code of Conduct while doing so.
|
||||
|
||||
Explicit enforcement of the Code of Conduct applies to the official mediums operated by the Elixir project:
|
||||
|
||||
* The [official GitHub projects][1] and code reviews.
|
||||
* The official elixir-lang mailing lists.
|
||||
* The **[#elixir][2]** IRC channel on [Libera.Chat][3].
|
||||
|
||||
Other Elixir activities (such as conferences, meetups, and unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
|
||||
Project maintainers may block, remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
|
||||
|
||||
**The goal of the Code of Conduct is to resolve conflicts in the most harmonious way possible**. We hope that in most cases issues may be resolved through polite discussion and mutual agreement. Bannings and other forceful measures are to be employed only as a last resort. **Do not** post about the issue publicly or try to rally sentiment against a particular individual or group.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
This document was based on the Code of Conduct from the Go project (dated Sep/2021) and the Contributor Covenant (v1.4).
|
||||
|
||||
[1]: https://github.com/elixir-lang/
|
||||
[2]: https://web.libera.chat/#elixir
|
||||
[3]: https://libera.chat/
|
||||
+265
@@ -0,0 +1,265 @@
|
||||
# Contributing to Elixir
|
||||
|
||||
Please take a moment to review this document in order to make the contribution
|
||||
process easy and effective for everyone involved!
|
||||
|
||||
## Using the issue tracker
|
||||
|
||||
Use the issues tracker for:
|
||||
|
||||
* [bug reports](#bugs-reports)
|
||||
* [submitting pull requests](#pull-requests)
|
||||
|
||||
Please **do not** use the issue tracker for personal support requests nor feature requests. Support requests should be sent to:
|
||||
|
||||
* [the elixir-talk mailing list](http://groups.google.com/group/elixir-lang-talk)
|
||||
* [Stack Overflow](http://stackoverflow.com/questions/ask?tags=elixir)
|
||||
* [#elixir-lang](irc://chat.freenode.net/elixir-lang)
|
||||
|
||||
Feature requests can be discussed on [the elixir-core mailing list](http://groups.google.com/group/elixir-lang-core).
|
||||
|
||||
We do our best to keep the issue tracker tidy and organized, making it useful
|
||||
for everyone. For example, we classify open issues per application and perceived
|
||||
difficulty of the issue, making it easier for developers to
|
||||
[contribute to Elixir](#contributing).
|
||||
|
||||
## Bug reports
|
||||
|
||||
A bug is a _demonstrable problem_ that is caused by the code in the repository.
|
||||
Good bug reports are extremely helpful - thank you!
|
||||
|
||||
Guidelines for bug reports:
|
||||
|
||||
1. **Use the GitHub issue search** — check if the issue has already been
|
||||
reported.
|
||||
|
||||
2. **Check if the issue has been fixed** — try to reproduce it using the
|
||||
`master` branch in the repository.
|
||||
|
||||
3. **Isolate and report the problem** — ideally create a reduced test
|
||||
case.
|
||||
|
||||
Please try to be as detailed as possible in your report. Include information about
|
||||
your Operating System, your Erlang and Elixir versions. Please provide steps to
|
||||
reproduce the issue as well as the outcome you were expecting! All these details
|
||||
will help developers to fix any potential bugs.
|
||||
|
||||
Example:
|
||||
|
||||
> Short and descriptive example bug report title
|
||||
>
|
||||
> A summary of the issue and the environment in which it occurs. If suitable,
|
||||
> include the steps required to reproduce the bug.
|
||||
>
|
||||
> 1. This is the first step
|
||||
> 2. This is the second step
|
||||
> 3. Further steps, etc.
|
||||
>
|
||||
> `<url>` - a link to the reduced test case (e.g. a GitHub Gist)
|
||||
>
|
||||
> Any other information you want to share that is relevant to the issue being
|
||||
> reported. This might include the lines of code that you have identified as
|
||||
> causing the bug, and potential solutions (and your opinions on their
|
||||
> merits).
|
||||
|
||||
## Feature requests
|
||||
|
||||
Feature requests are welcome and should be discussed on [the elixir-core mailing list](http://groups.google.com/group/elixir-lang-core). But take a moment to find
|
||||
out whether your idea fits with the scope and aims of the project. It's up to *you*
|
||||
to make a strong case to convince the community of the merits of this feature.
|
||||
Please provide as much detail and context as possible.
|
||||
|
||||
## Contributing
|
||||
|
||||
We incentivize everyone to contribute to Elixir and help us tackle
|
||||
existing issues! To do so, there are a few things you need to know
|
||||
about the code. First, Elixir code is divided in applications inside
|
||||
the `lib` folder:
|
||||
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` — IEx, Elixir's interactive shell
|
||||
|
||||
* `mix` — Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`.
|
||||
|
||||
In case you are changing a single file, you can compile and run tests only
|
||||
for that particular file for fast development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
$ bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
$ bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
|
||||
After your changes are done, please remember to run the full suite with
|
||||
`make test`.
|
||||
|
||||
From time to time, your tests may fail in an existing Elixir checkout and
|
||||
may require a clean start by running `make clean compile`. You can always
|
||||
check [the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
send your pull requests.
|
||||
|
||||
## Contributing Documentation
|
||||
|
||||
Code documentation (`@doc`, `@moduledoc`, `@typedoc`) has a special convention:
|
||||
the first paragraph is considered to be a short summary.
|
||||
|
||||
For functions, macros and callbacks say what it will do. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is true.
|
||||
|
||||
...
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
For modules, protocols and types say what it is. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
Information about a file.
|
||||
|
||||
...
|
||||
"""
|
||||
|
||||
defstruct [...]
|
||||
end
|
||||
```
|
||||
|
||||
Keep in mind that the first paragraph might show up in a summary somewhere, long
|
||||
texts in the first paragraph create very ugly summaries. As a rule of thumb
|
||||
anything longer than 80 characters is too long.
|
||||
|
||||
Try to keep unneccesary details out of the first paragraph, it's only there to
|
||||
give a user a quick idea of what the documented "thing" does/is. The rest of the
|
||||
documentation string can contain the details, for example when a value and when
|
||||
`nil` is returned.
|
||||
|
||||
If possible include examples, preferably in a form that works with doctests. For
|
||||
example:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Return only those elements for which `fun` is true.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Enum.filter([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
|
||||
[2]
|
||||
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
This makes it easy to test the examples so that they don't go stale and examples
|
||||
are often a great help in explaining what a function does.
|
||||
|
||||
## Pull requests
|
||||
|
||||
Good pull requests - patches, improvements, new features - are a fantastic
|
||||
help. They should remain focused in scope and avoid containing unrelated
|
||||
commits.
|
||||
|
||||
**IMPORTANT**: By submitting a patch, you agree that your work will be
|
||||
licensed under the license used by the project.
|
||||
|
||||
If you have any large pull request in mind (e.g. implementing features,
|
||||
refactoring code, etc), **please ask first** otherwise you risk spending
|
||||
a lot of time working on something that the project's developers might
|
||||
not want to merge into the project.
|
||||
|
||||
Please adhere to the coding conventions in the project (indentation,
|
||||
accurate comments, etc.) and don't forget to add your own tests and
|
||||
documentation. When working with git, we recommend the following process
|
||||
in order to craft an excellent pull request:
|
||||
|
||||
1. [Fork](http://help.github.com/fork-a-repo/) the project, clone your fork,
|
||||
and configure the remotes:
|
||||
|
||||
```bash
|
||||
# Clone your fork of the repo into the current directory
|
||||
git clone https://github.com/<your-username>/elixir
|
||||
# Navigate to the newly cloned directory
|
||||
cd elixir
|
||||
# Assign the original repo to a remote called "upstream"
|
||||
git remote add upstream https://github.com/elixir-lang/elixir
|
||||
```
|
||||
|
||||
2. If you cloned a while ago, get the latest changes from upstream:
|
||||
|
||||
```bash
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
```
|
||||
|
||||
3. Create a new topic branch (off of `master`) to contain your feature, change,
|
||||
or fix.
|
||||
|
||||
**IMPORTANT**: Making changes in `master` is discouraged. You should always
|
||||
keep your local `master` in sync with upstream `master` and make your
|
||||
changes in topic branches.
|
||||
|
||||
```bash
|
||||
git checkout -b <topic-branch-name>
|
||||
```
|
||||
|
||||
4. Commit your changes in logical chunks. Keep your commit messages organized,
|
||||
with a short description in the first line and more detailed information on
|
||||
the following lines. Feel free to use Git's
|
||||
[interactive rebase](https://help.github.com/articles/interactive-rebase)
|
||||
feature to tidy up your commits before making them public.
|
||||
|
||||
5. Make sure all the tests are still passing.
|
||||
|
||||
```bash
|
||||
make test
|
||||
```
|
||||
|
||||
This command will compile the code in your branch and use that
|
||||
version of Elixir to run the tests. This is needed to ensure your changes can
|
||||
pass all the tests.
|
||||
|
||||
6. Push your topic branch up to your fork:
|
||||
|
||||
```bash
|
||||
git push origin <topic-branch-name>
|
||||
```
|
||||
|
||||
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
|
||||
with a clear title and description.
|
||||
|
||||
8. If you haven't updated your pull request for a while, you should consider
|
||||
rebasing on master and resolving any conflicts.
|
||||
|
||||
**IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
|
||||
should always `git rebase` on `master` to bring your changes up to date when
|
||||
necessary.
|
||||
|
||||
```bash
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
git checkout <your-topic-branch>
|
||||
git rebase master
|
||||
```
|
||||
|
||||
We have saved some excellent pull requests we have received in the past in case
|
||||
you are looking for some examples:
|
||||
|
||||
* https://github.com/elixir-lang/elixir/pull/992
|
||||
* https://github.com/elixir-lang/elixir/pull/1058
|
||||
* https://github.com/elixir-lang/elixir/pull/1059
|
||||
|
||||
Thank you for your contributions!
|
||||
@@ -0,0 +1,8 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are covered under either Elixir's
|
||||
license (see the file LICENSE) except the files mentioned below that
|
||||
contains sections that are under Erlang's License (EPL):
|
||||
|
||||
lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
@@ -1,176 +1,13 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
Copyright 2012 Plataformatec
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
1. Definitions.
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,111 +1,94 @@
|
||||
PREFIX ?= /usr/local
|
||||
TEST_FILES ?= "*_test.exs"
|
||||
SHARE_PREFIX ?= $(PREFIX)/share
|
||||
MAN_PREFIX ?= $(SHARE_PREFIX)/man
|
||||
# CANONICAL := main/
|
||||
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
|
||||
REBAR := rebar
|
||||
DOCS := v1.0
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
ERL_MAKE := if [ -n "$(ERLC_OPTS)" ]; then ERL_COMPILER_OPTIONS=$(ERLC_OPTS) erl -make; else erl -make; fi
|
||||
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
||||
GENERATE_APP := $(CURDIR)/lib/elixir/scripts/generate_app.escript
|
||||
VERSION := $(strip $(shell cat VERSION))
|
||||
Q := @
|
||||
PREFIX := /usr/local
|
||||
LIBDIR := lib
|
||||
BINDIR := bin
|
||||
INSTALL = install
|
||||
INSTALL_DIR = $(INSTALL) -m755 -d
|
||||
INSTALL_DATA = $(INSTALL) -m644
|
||||
INSTALL_PROGRAM = $(INSTALL) -m755
|
||||
GIT_REVISION = $(strip $(shell git rev-parse HEAD 2> /dev/null ))
|
||||
GIT_TAG = $(strip $(shell head="$(call GIT_REVISION)"; git tag --points-at $$head 2> /dev/null | tail -1) )
|
||||
SOURCE_DATE_EPOCH_PATH = lib/elixir/tmp/ebin_reproducible
|
||||
SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
|
||||
|
||||
.PHONY: install compile erlang elixir unicode app build_plt clean_plt dialyze test check_reproducible clean clean_residual_files format install_man clean_man docs Docs.zip Precompiled.zip zips
|
||||
.NOTPARALLEL:
|
||||
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean docs release_docs release_zip check_erlang_release
|
||||
.NOTPARALLEL: compile
|
||||
|
||||
#==> Functions
|
||||
|
||||
# This check should work for older versions like R16B
|
||||
# as well as new verions like 17.1 and 18
|
||||
define CHECK_ERLANG_RELEASE
|
||||
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 25)])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang/OTP 25.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi
|
||||
$(Q) erl -noshell -eval 'io:fwrite("~s", [erlang:system_info(otp_release)])' -s erlang halt | grep -q '^1[789]'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang 17.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
endef
|
||||
|
||||
define APP_TEMPLATE
|
||||
$(1): lib/$(1)/ebin/Elixir.$(2).beam lib/$(1)/ebin/$(1).app
|
||||
|
||||
lib/$(1)/ebin/$(1).app: lib/$(1)/mix.exs
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -e 'Mix.start(:permanent, [])' -r mix.exs -e 'Mix.Task.run("compile.app", ~w[--compile-path ebin])'
|
||||
$(Q) mkdir -p lib/$(1)/_build/shared/lib/$(1)
|
||||
$(Q) cp -R lib/$(1)/ebin lib/$(1)/_build/shared/lib/$(1)/
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -e 'Mix.start(:permanent, [])' -r mix.exs -e 'Mix.Task.run("compile.app")'
|
||||
$(Q) cp lib/$(1)/_build/shared/lib/$(1)/ebin/$(1).app lib/$(1)/ebin/$(1).app
|
||||
$(Q) rm -rf lib/$(1)/_build
|
||||
|
||||
lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex)
|
||||
@ echo "==> $(1) (compile)"
|
||||
@ rm -rf lib/$(1)/ebin
|
||||
$(Q) cd lib/$(1) && ../../$$(ELIXIRC) "lib/**/*.ex" -o ebin
|
||||
|
||||
test_$(1): test_formatted $(1)
|
||||
@ echo "==> $(1) (ex_unit)"
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/$(TEST_FILES)";
|
||||
endef
|
||||
|
||||
define WRITE_SOURCE_DATE_EPOCH
|
||||
$(shell mkdir -p $(SOURCE_DATE_EPOCH_PATH) && bin/elixir -e \
|
||||
'IO.puts System.build_info()[:date] \
|
||||
|> DateTime.from_iso8601() \
|
||||
|> elem(1) \
|
||||
|> DateTime.to_unix()' > $(SOURCE_DATE_EPOCH_FILE))
|
||||
endef
|
||||
|
||||
define READ_SOURCE_DATE_EPOCH
|
||||
$(strip $(shell cat $(SOURCE_DATE_EPOCH_FILE)))
|
||||
test_$(1): $(1)
|
||||
@ echo "==> $(1) (exunit)"
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs";
|
||||
endef
|
||||
|
||||
#==> Compilation tasks
|
||||
|
||||
APP := lib/elixir/ebin/elixir.app
|
||||
PARSER := lib/elixir/src/elixir_parser.erl
|
||||
KERNEL := lib/elixir/ebin/Elixir.Kernel.beam
|
||||
UNICODE := lib/elixir/ebin/Elixir.String.Unicode.beam
|
||||
KERNEL:=lib/elixir/ebin/Elixir.Kernel.beam
|
||||
UNICODE:=lib/elixir/ebin/Elixir.String.Unicode.beam
|
||||
|
||||
default: compile
|
||||
|
||||
compile: erlang $(APP) elixir
|
||||
compile: lib/elixir/src/elixir.app.src erlang elixir
|
||||
|
||||
erlang: $(PARSER)
|
||||
$(Q) if [ ! -f $(APP) ]; then $(call CHECK_ERLANG_RELEASE); fi
|
||||
$(Q) cd lib/elixir && mkdir -p ebin && $(ERL_MAKE)
|
||||
lib/elixir/src/elixir.app.src: src/elixir.app.src
|
||||
$(Q) $(call CHECK_ERLANG_RELEASE)
|
||||
$(Q) rm -rf lib/elixir/src/elixir.app.src
|
||||
$(Q) echo "%% This file is automatically generated from <project_root>/src/elixir.app.src" \
|
||||
>lib/elixir/src/elixir.app.src
|
||||
$(Q) cat src/elixir.app.src >>lib/elixir/src/elixir.app.src
|
||||
|
||||
$(PARSER): lib/elixir/src/elixir_parser.yrl
|
||||
$(Q) erlc -o $@ +'{verbose,true}' +'{report,true}' $<
|
||||
erlang:
|
||||
$(Q) cd lib/elixir && ../../$(REBAR) compile
|
||||
|
||||
# Since Mix depends on EEx and EEx depends on Mix,
|
||||
# we first compile EEx without the .app file,
|
||||
# then Mix, and then compile EEx fully
|
||||
# Since Mix depends on EEx and EEx depends on
|
||||
# Mix, we first compile EEx without the .app
|
||||
# file, then mix and then compile EEx fully
|
||||
elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
|
||||
|
||||
stdlib: $(KERNEL) VERSION
|
||||
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
echo "==> bootstrap (compile)"; \
|
||||
$(ERL) -s elixir_compiler bootstrap -s erlang halt; \
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
echo "==> bootstrap (compile)"; \
|
||||
$(ERL) -s elixir_compiler core -s erlang halt; \
|
||||
fi
|
||||
$(Q) "$(MAKE)" unicode
|
||||
@ echo "==> elixir (compile)";
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/kernel.ex" -o ebin;
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
|
||||
$(Q) "$(MAKE)" app
|
||||
|
||||
app: $(APP)
|
||||
$(APP): lib/elixir/src/elixir.app.src lib/elixir/ebin VERSION $(GENERATE_APP)
|
||||
$(Q) $(GENERATE_APP) $< $@ $(VERSION)
|
||||
$(Q) $(MAKE) unicode
|
||||
$(Q) rm -rf lib/elixir/ebin/elixir.app
|
||||
$(Q) cd lib/elixir && ../../$(REBAR) compile
|
||||
|
||||
unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@ echo "==> unicode (compile)";
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/security.ex -o lib/elixir/ebin;
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
|
||||
@ echo "Embedding the Unicode database... (this may take a while)"
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) unicode/unicode.ex -o ebin;
|
||||
|
||||
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
|
||||
$(eval $(call APP_TEMPLATE,logger,Logger))
|
||||
@@ -116,153 +99,74 @@ $(eval $(call APP_TEMPLATE,iex,IEx))
|
||||
install: compile
|
||||
@ echo "==> elixir (install)"
|
||||
$(Q) for dir in lib/*; do \
|
||||
rm -rf $(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin; \
|
||||
$(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin"; \
|
||||
$(INSTALL_DATA) $$dir/ebin/* "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin"; \
|
||||
done
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/bin"
|
||||
$(Q) $(INSTALL_PROGRAM) $(filter-out %.ps1, $(filter-out %.bat, $(wildcard bin/*))) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/bin"
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(BINDIR)"
|
||||
$(Q) for file in "$(DESTDIR)$(PREFIX)"/$(LIBDIR)/elixir/bin/*; do \
|
||||
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/$(BINDIR)/"; \
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/bin"
|
||||
$(Q) for file in "$(DESTDIR)$(PREFIX)"/$(LIBDIR)/elixir/bin/* ; do \
|
||||
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/bin/" ; \
|
||||
done
|
||||
"$(MAKE)" install_man
|
||||
|
||||
check_reproducible: compile
|
||||
$(Q) echo "==> Checking for reproducible builds..."
|
||||
$(Q) rm -rf lib/*/tmp/ebin_reproducible/
|
||||
$(call WRITE_SOURCE_DATE_EPOCH)
|
||||
$(Q) mkdir -p lib/elixir/tmp/ebin_reproducible/ \
|
||||
lib/eex/tmp/ebin_reproducible/ \
|
||||
lib/ex_unit/tmp/ebin_reproducible/ \
|
||||
lib/iex/tmp/ebin_reproducible/ \
|
||||
lib/logger/tmp/ebin_reproducible/ \
|
||||
lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/elixir/ebin/* lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/eex/ebin/* lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/ex_unit/ebin/* lib/ex_unit/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/iex/ebin/* lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/logger/ebin/* lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/mix/ebin/* lib/mix/tmp/ebin_reproducible/
|
||||
SOURCE_DATE_EPOCH=$(call READ_SOURCE_DATE_EPOCH) "$(MAKE)" compile
|
||||
$(Q) echo "Diffing..."
|
||||
$(Q) bin/elixir lib/elixir/scripts/diff.exs lib/elixir/ebin/ lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/scripts/diff.exs lib/eex/ebin/ lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/scripts/diff.exs lib/ex_unit/ebin/ lib/ex_unit/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/scripts/diff.exs lib/iex/ebin/ lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/scripts/diff.exs lib/logger/ebin/ lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/scripts/diff.exs lib/mix/ebin/ lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) echo "Builds are reproducible"
|
||||
|
||||
clean:
|
||||
cd lib/elixir && ../../$(REBAR) clean
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
rm -rf $(PARSER)
|
||||
$(Q) "$(MAKE)" clean_residual_files
|
||||
rm -rf lib/elixir/test/ebin
|
||||
rm -rf lib/*/tmp
|
||||
rm -rf lib/mix/test/fixtures/git_repo
|
||||
rm -rf lib/mix/test/fixtures/deps_on_git_repo
|
||||
rm -rf lib/mix/test/fixtures/git_rebar
|
||||
rm -rf lib/elixir/src/elixir.app.src
|
||||
|
||||
clean_elixir:
|
||||
clean_exbeam:
|
||||
$(Q) rm -f lib/*/ebin/Elixir.*.beam
|
||||
|
||||
clean_residual_files:
|
||||
rm -rf lib/*/_build/
|
||||
rm -rf lib/*/tmp/
|
||||
rm -rf lib/elixir/test/ebin/
|
||||
rm -rf lib/mix/test/fixtures/deps_on_git_repo/
|
||||
rm -rf lib/mix/test/fixtures/git_rebar/
|
||||
rm -rf lib/mix/test/fixtures/git_repo/
|
||||
rm -rf lib/mix/test/fixtures/git_sparse_repo/
|
||||
rm -rf lib/mix/test/fixtures/archive/ebin/
|
||||
rm -f erl_crash.dump
|
||||
$(Q) "$(MAKE)" clean_man
|
||||
#==> Release tasks
|
||||
|
||||
#==> Documentation tasks
|
||||
SOURCE_REF = $(shell head="$$(git rev-parse HEAD)" tag="$$(git tag --points-at $$head | tail -1)" ; echo "$${tag:-$$head}\c")
|
||||
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" -o doc/$(2) -p http://elixir-lang.org/docs.html
|
||||
|
||||
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}")
|
||||
DOCS_COMPILE = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" --logo lib/elixir/pages/images/logo.png --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" $(4)
|
||||
DOCS_CONFIG = bin/elixir lib/elixir/scripts/docs_config.exs "$(1)"
|
||||
|
||||
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
|
||||
|
||||
docs_elixir: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (elixir)"
|
||||
$(Q) rm -rf doc/elixir
|
||||
$(call DOCS_COMPILE,Elixir,elixir,Kernel,--config "lib/elixir/scripts/elixir_docs.exs")
|
||||
$(call DOCS_CONFIG,elixir)
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
$(Q) rm -rf doc/eex
|
||||
$(call DOCS_COMPILE,EEx,eex,EEx,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,eex)
|
||||
|
||||
docs_mix: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (mix)"
|
||||
$(Q) rm -rf doc/mix
|
||||
$(call DOCS_COMPILE,Mix,mix,Mix,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,mix)
|
||||
|
||||
docs_iex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (iex)"
|
||||
$(Q) rm -rf doc/iex
|
||||
$(call DOCS_COMPILE,IEx,iex,IEx,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,iex)
|
||||
|
||||
docs_ex_unit: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (ex_unit)"
|
||||
$(Q) rm -rf doc/ex_unit
|
||||
$(call DOCS_COMPILE,ExUnit,ex_unit,ExUnit,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,ex_unit)
|
||||
|
||||
docs_logger: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (logger)"
|
||||
$(Q) rm -rf doc/logger
|
||||
$(call DOCS_COMPILE,Logger,logger,Logger,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,logger)
|
||||
docs: compile ../ex_doc/bin/ex_doc
|
||||
$(Q) rm -rf doc
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel)
|
||||
$(call COMPILE_DOCS,EEx,eex,EEx)
|
||||
$(call COMPILE_DOCS,Mix,mix,Mix)
|
||||
$(call COMPILE_DOCS,IEx,iex,IEx)
|
||||
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit)
|
||||
$(call COMPILE_DOCS,Logger,logger,Logger)
|
||||
|
||||
../ex_doc/bin/ex_doc:
|
||||
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
|
||||
@ false
|
||||
|
||||
#==> Zip tasks
|
||||
release_zip: compile
|
||||
rm -rf v$(VERSION).zip
|
||||
zip -9 -r v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE Makefile README.md VERSION
|
||||
|
||||
Docs.zip: docs
|
||||
rm -f Docs.zip
|
||||
zip -9 -r Docs.zip CHANGELOG.md doc NOTICE LICENSE README.md
|
||||
@ echo "Docs file created $(CURDIR)/Docs.zip"
|
||||
release_docs: docs
|
||||
rm -rf ../docs/$(DOCS)/*/
|
||||
mv doc/* ../docs/$(DOCS)
|
||||
|
||||
Precompiled.zip: build_man compile
|
||||
rm -f Precompiled.zip
|
||||
zip -9 -r Precompiled.zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE Makefile man NOTICE README.md VERSION
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled.zip"
|
||||
# This task requires aws-cli to be installed and set up for access to s3.hex.pm
|
||||
# See: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html
|
||||
|
||||
#==> Test tasks
|
||||
publish_mix: compile
|
||||
cd lib/mix && MIX_ENV=prod mix escript.build
|
||||
aws s3 cp lib/mix/mix s3://s3.hex.pm/builds/mix/v$(VERSION)/mix --acl public-read
|
||||
aws s3 cp lib/mix/mix s3://s3.hex.pm/builds/mix/mix --acl public-read
|
||||
rm lib/mix/mix
|
||||
rm -rf lib/mix/_build
|
||||
|
||||
test: test_formatted test_erlang test_elixir
|
||||
#==> Tests tasks
|
||||
|
||||
test_windows: test test_taskkill
|
||||
|
||||
test_taskkill:
|
||||
taskkill //IM erl.exe //F //T //FI "MEMUSAGE gt 0"
|
||||
taskkill //IM epmd.exe //F //T //FI "MEMUSAGE gt 0"
|
||||
test: test_erlang test_elixir
|
||||
|
||||
TEST_ERL = lib/elixir/test/erlang
|
||||
TEST_EBIN = lib/elixir/test/ebin
|
||||
TEST_ERLS = $(addprefix $(TEST_EBIN)/, $(addsuffix .beam, $(basename $(notdir $(wildcard $(TEST_ERL)/*.erl)))))
|
||||
|
||||
define FORMAT
|
||||
$(Q) if [ "$(OS)" = "Windows_NT" ]; then \
|
||||
cmd //C call ./bin/mix.bat format $(1); \
|
||||
else \
|
||||
bin/elixir bin/mix format $(1); \
|
||||
fi
|
||||
endef
|
||||
|
||||
format: compile
|
||||
$(call FORMAT)
|
||||
|
||||
test_formatted: compile
|
||||
$(call FORMAT,--check-formatted)
|
||||
|
||||
test_erlang: compile $(TEST_ERLS)
|
||||
@ echo "==> elixir (eunit)"
|
||||
$(Q) $(ERL) -pa $(TEST_EBIN) -s test_helper test;
|
||||
@@ -272,15 +176,19 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
|
||||
$(Q) mkdir -p $(TEST_EBIN)
|
||||
$(Q) $(ERLC) -o $(TEST_EBIN) $<
|
||||
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_eex test_iex test_mix
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_doc_test test_mix test_eex test_iex
|
||||
|
||||
test_doc_test: compile
|
||||
@ echo "==> doctest (exunit)"
|
||||
$(Q) cd lib/elixir && ../../bin/elixir -r "test/doc_test.exs";
|
||||
|
||||
test_stdlib: compile
|
||||
@ echo "==> elixir (ex_unit)"
|
||||
@ echo "==> elixir (exunit)"
|
||||
$(Q) exec epmd & exit
|
||||
$(Q) if [ "$(OS)" = "Windows_NT" ]; then \
|
||||
cd lib/elixir && cmd //C call ../../bin/elixir.bat --sname primary -r "test/elixir/test_helper.exs" -pr "test/elixir/**/$(TEST_FILES)"; \
|
||||
cd lib/elixir && cmd //C call ../../bin/elixir.bat -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
|
||||
else \
|
||||
cd lib/elixir && ../../bin/elixir --sname primary -r "test/elixir/test_helper.exs" -pr "test/elixir/**/$(TEST_FILES)"; \
|
||||
cd lib/elixir && ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
|
||||
fi
|
||||
|
||||
#==> Dialyzer tasks
|
||||
@@ -290,7 +198,7 @@ PLT = .elixir.plt
|
||||
|
||||
$(PLT):
|
||||
@ echo "==> Building PLT with Elixir's dependencies..."
|
||||
$(Q) dialyzer --output_plt $(PLT) --build_plt --apps erts kernel stdlib compiler syntax_tools parsetools tools ssl inets crypto runtime_tools ftp tftp mnesia public_key asn1 sasl
|
||||
$(Q) dialyzer --output_plt $(PLT) --build_plt --apps erts kernel stdlib compiler syntax_tools parsetools tools ssl inets
|
||||
|
||||
clean_plt:
|
||||
$(Q) rm -f $(PLT)
|
||||
@@ -299,34 +207,4 @@ build_plt: clean_plt $(PLT)
|
||||
|
||||
dialyze: compile $(PLT)
|
||||
@ echo "==> Dialyzing Elixir..."
|
||||
$(Q) dialyzer -pa lib/elixir/ebin --plt $(PLT) $(DIALYZER_OPTS) lib/*/ebin
|
||||
|
||||
#==> Man page tasks
|
||||
|
||||
build_man: man/iex.1 man/elixir.1
|
||||
|
||||
man/iex.1:
|
||||
$(Q) cp man/iex.1.in man/iex.1
|
||||
$(Q) sed -i.bak "/{COMMON}/r man/common" man/iex.1
|
||||
$(Q) sed -i.bak "/{COMMON}/d" man/iex.1
|
||||
$(Q) rm -f man/iex.1.bak
|
||||
|
||||
man/elixir.1:
|
||||
$(Q) cp man/elixir.1.in man/elixir.1
|
||||
$(Q) sed -i.bak "/{COMMON}/r man/common" man/elixir.1
|
||||
$(Q) sed -i.bak "/{COMMON}/d" man/elixir.1
|
||||
$(Q) rm -f man/elixir.1.bak
|
||||
|
||||
clean_man:
|
||||
rm -f man/elixir.1
|
||||
rm -f man/elixir.1.bak
|
||||
rm -f man/iex.1
|
||||
rm -f man/iex.1.bak
|
||||
|
||||
install_man: build_man
|
||||
$(Q) mkdir -p $(DESTDIR)$(MAN_PREFIX)/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(MAN_PREFIX)/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(MAN_PREFIX)/man1
|
||||
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(MAN_PREFIX)/man1
|
||||
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(MAN_PREFIX)/man1
|
||||
"$(MAKE)" clean_man
|
||||
$(Q) dialyzer --plt $(PLT) $(DIALYZER_OPTS) lib/*/ebin
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are copyright to the terms below.
|
||||
|
||||
== lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
|
||||
Copyright Ericsson AB 1996-2015
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
== All other files
|
||||
|
||||
Copyright 2012 Plataformatec
|
||||
Copyright 2021 The Elixir Team
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,251 +1,59 @@
|
||||
<h1>
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/elixir-lang/elixir-lang.github.com/raw/main/images/logo/logo-dark.png">
|
||||
<img alt="Elixir logo" src="https://github.com/elixir-lang/elixir-lang.github.com/raw/main/images/logo/logo.png" width="200">
|
||||
</picture>
|
||||
</h1>
|
||||

|
||||
=========
|
||||
[](http://travis-ci.org/elixir-lang/elixir)
|
||||
|
||||
[](https://github.com/elixir-lang/elixir/actions/workflows/ci.yml?query=branch%3Amain)
|
||||
For more about Elixir, installation and documentation, [check Elixir's website](http://elixir-lang.org/).
|
||||
|
||||
Elixir is a dynamic, functional language designed for building scalable
|
||||
and maintainable applications.
|
||||
## Usage
|
||||
|
||||
For more about Elixir, installation and documentation,
|
||||
[check Elixir's website](https://elixir-lang.org/).
|
||||
If you want to contribute to Elixir or run it from source, clone this repository to your machine, compile and test it:
|
||||
|
||||
## Policies
|
||||
$ git clone https://github.com/elixir-lang/elixir.git
|
||||
$ cd elixir
|
||||
$ make clean test
|
||||
|
||||
New releases are announced in the [announcement mailing list][8].
|
||||
You can subscribe by sending an email to <elixir-lang-ann+subscribe@googlegroups.com>
|
||||
and replying to the confirmation email.
|
||||
> Note: if you are running on Windows, [this article includes important notes for compiling Elixir from source on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
|
||||
All security releases [will be tagged with `[security]`][10]. For more
|
||||
information, please read our [Security Policy][9].
|
||||
If Elixir fails to build (specifically when pulling in a new version via git), be sure to remove any previous build artifacts by running `make clean`, then `make test`.
|
||||
|
||||
All interactions in our official communication channels follow our
|
||||
[Code of Conduct][1].
|
||||
If tests pass, you are ready to move on to the [Getting Started guide][1] or to try Interactive Elixir by running: `bin/iex` in your terminal.
|
||||
|
||||
## Bug reports
|
||||
However, if tests fail, it is likely you have an outdated Erlang version (Elixir requires Erlang 17.0 or later). You can check your Erlang version by calling `erl` in the command line. You will see some information as follows:
|
||||
|
||||
For reporting bugs, [visit our issue tracker][2] and follow the steps
|
||||
for reporting a new issue. **Please disclose security vulnerabilities
|
||||
privately at <elixir-security@googlegroups.com>**.
|
||||
Erlang/OTP 17 [erts-6.0] [source-07b8f44] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
|
||||
|
||||
## Issues tracker management
|
||||
|
||||
All currently open bugs related to the Elixir repository are listed
|
||||
in the issues tracker. The Elixir team uses the issues tracker to focus
|
||||
on *actionable items*, including planned enhancements in the short and
|
||||
medium term. We also do our best to label entries for clarity and to ease
|
||||
collaboration.
|
||||
|
||||
Our *actionable item policy* has some important consequences, such as:
|
||||
|
||||
* Proposing new features as well as requests for support, help, and
|
||||
guidance must be done in their own spaces, detailed next.
|
||||
|
||||
* Issues we have identified to be outside of Elixir's scope,
|
||||
such as an upstream bug, will be closed (and requested to be moved
|
||||
elsewhere if appropriate).
|
||||
|
||||
* We actively close unrelated and non-actionable issues to keep the
|
||||
issues tracker tidy. We may get things wrong from time to
|
||||
time and will gladly revisit issues, reopening when necessary.
|
||||
|
||||
Keep the tone positive and be kind! For more information, see the
|
||||
[Code of Conduct][1].
|
||||
|
||||
### Proposing new features
|
||||
|
||||
For proposing new features, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. The [language development history and
|
||||
its focus are described on our website](https://elixir-lang.org/development.html).
|
||||
|
||||
Keep in mind that it is your responsibility to argue and explain
|
||||
why a feature is useful and how it will impact the codebase and
|
||||
the community. A good proposal includes the problem description
|
||||
and how the proposed solution compares with existing alternatives
|
||||
in the Elixir ecosystem (as well as in other languages). To iron
|
||||
out a proposal before submission, consider using and gathering
|
||||
feedback from the community spaces [listed on the sidebar of the
|
||||
Elixir website](https://elixir-lang.org/).
|
||||
|
||||
Once a proposal is accepted, it will be added to [the issue tracker][2].
|
||||
Features and bug fixes that have already been merged and will be included
|
||||
in the next release are then "closed" and added to the [changelog][7].
|
||||
|
||||
### Discussions, support, and help
|
||||
|
||||
For general discussions, support, and help, please use the community
|
||||
spaces [listed on the sidebar of the Elixir website](https://elixir-lang.org/),
|
||||
such as forums, chat platforms, etc, where the wider community will be available
|
||||
to help you.
|
||||
|
||||
## Compiling from source
|
||||
|
||||
For the many different ways to install Elixir,
|
||||
[see our installation instructions on the website](https://elixir-lang.org/install.html).
|
||||
However, if you want to contribute to Elixir, you will need to compile from source.
|
||||
|
||||
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang).
|
||||
After that, clone this repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
cd elixir
|
||||
make
|
||||
```
|
||||
|
||||
> Note: if you are running on Windows,
|
||||
[this article includes important notes for compiling Elixir from source
|
||||
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
|
||||
In case you want to use this Elixir version as your system version,
|
||||
you need to add the `bin` directory to [your PATH environment variable](https://elixir-lang.org/install.html#setting-path-environment-variable).
|
||||
|
||||
Additionally, you may choose to run the test suite with `make clean test`.
|
||||
|
||||
## Contributing
|
||||
|
||||
We invite contributions to Elixir. To contribute, there are a few
|
||||
things you need to know about the code. First, Elixir code is divided
|
||||
by each application inside the `lib` folder:
|
||||
|
||||
* `elixir` - Elixir's kernel and standard library
|
||||
|
||||
* `eex` - EEx is the template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - ExUnit is a simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx stands for Interactive Elixir: Elixir's interactive shell
|
||||
|
||||
* `logger` - Logger is the built-in logger
|
||||
|
||||
* `mix` - Mix is Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test`. You can
|
||||
also run tests for a specific framework with `make test_#{APPLICATION}`, for example,
|
||||
`make test_ex_unit`. If you just changed something in Elixir's standard
|
||||
library, you can run only that portion through `make test_stdlib`.
|
||||
|
||||
If you are only changing one file, you can choose to compile and run tests
|
||||
for that specific file for faster development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
```sh
|
||||
bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
```
|
||||
|
||||
You can also use the `LINE` env var to run a single test:
|
||||
|
||||
```sh
|
||||
LINE=123 bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
````
|
||||
|
||||
To recompile (including Erlang modules):
|
||||
|
||||
```sh
|
||||
make compile
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run `make format` to guarantee
|
||||
all files are properly formatted, then run the full suite with
|
||||
`make test`.
|
||||
|
||||
If your contribution fails during the bootstrapping of the language,
|
||||
you can rebuild the language from scratch with:
|
||||
|
||||
```sh
|
||||
make clean_elixir compile
|
||||
```
|
||||
|
||||
Similarly, if you can not get Elixir to compile or the tests to pass after
|
||||
updating an existing checkout, run `make clean compile`. You can check
|
||||
[the official build status](https://github.com/elixir-lang/elixir/actions/workflows/ci.yml).
|
||||
More tasks can be found by reading the [Makefile](Makefile).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
[send a pull request](https://help.github.com/articles/using-pull-requests/).
|
||||
We have saved some excellent pull requests we have received in the past in
|
||||
case you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? - Pull request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? - Pull request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit - Pull request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
### Reviewing changes
|
||||
|
||||
Once a pull request is sent, the Elixir team will review your changes.
|
||||
We outline our process below to clarify the roles of everyone involved.
|
||||
|
||||
All pull requests must be approved by two committers before being merged into
|
||||
the repository. If changes are necessary, the team will leave appropriate
|
||||
comments requesting changes to the code. Unfortunately, we cannot guarantee a
|
||||
pull request will be merged, even when modifications are requested, as the Elixir
|
||||
team will re-evaluate the contribution as it changes.
|
||||
|
||||
Committers may also push style changes directly to your branch. If you would
|
||||
rather manage all changes yourself, you can disable the "Allow edits from maintainers"
|
||||
feature when submitting your pull request.
|
||||
|
||||
The Elixir team may optionally assign someone to review a pull request.
|
||||
If someone is assigned, they must explicitly approve the code before
|
||||
another team member can merge it.
|
||||
|
||||
When the review finishes, your pull request will be squashed and merged
|
||||
into the repository. If you have carefully organized your commits and
|
||||
believe they should be merged without squashing, please mention it in
|
||||
a comment.
|
||||
If you have the correct version and tests still fail, feel free to [open an issue][2].
|
||||
|
||||
## Building documentation
|
||||
|
||||
Building the documentation requires that [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
is installed and built alongside Elixir:
|
||||
Building the documentation requires [ex_doc](https://github.com/elixir-lang/ex_doc) to be installed and built in the same containing folder as elixir.
|
||||
|
||||
```sh
|
||||
# After cloning and compiling Elixir, in its parent directory:
|
||||
git clone https://github.com/elixir-lang/ex_doc.git
|
||||
cd ex_doc && ../elixir/bin/elixir ../elixir/bin/mix do deps.get + compile
|
||||
```
|
||||
# After cloning and compiling Elixir
|
||||
$ git clone git://github.com/elixir-lang/ex_doc.git
|
||||
$ cd ex_doc && ../elixir/bin/mix compile
|
||||
$ cd ../elixir && make docs
|
||||
|
||||
Now go back to Elixir's root directory and run:
|
||||
## Contributing
|
||||
|
||||
```sh
|
||||
make docs # to generate HTML pages
|
||||
make docs DOCS_FORMAT=epub # to generate EPUB documents
|
||||
```
|
||||
We appreciate any contribution to Elixir, so check out our [CONTRIBUTING.md](CONTRIBUTING.md) guide for more information. We usually keep a list of features and bugs [in the issue tracker][2].
|
||||
|
||||
This will produce documentation sets for `elixir`, `eex`, `ex_unit`, `iex`, `logger`,
|
||||
and `mix` under the `doc` directory. If you are planning to contribute documentation,
|
||||
[please check our best practices for writing documentation](https://hexdocs.pm/elixir/writing-documentation.html).
|
||||
## Important links
|
||||
|
||||
## Development links
|
||||
* \#elixir-lang on freenode IRC
|
||||
* [Website][1]
|
||||
* [Issue tracker][2]
|
||||
* [elixir-talk Mailing list (questions)][3]
|
||||
* [elixir-core Mailing list (development)][4]
|
||||
|
||||
* [Elixir Documentation][6]
|
||||
* [Elixir Core Mailing list (development)][3]
|
||||
* [Announcement mailing list][8]
|
||||
* [Code of Conduct][1]
|
||||
* [Issue tracker][2]
|
||||
* [Changelog][7]
|
||||
* [Security Policy][9]
|
||||
* **[#elixir][4]** on [Libera.Chat][5] IRC
|
||||
|
||||
[1]: CODE_OF_CONDUCT.md
|
||||
[1]: http://elixir-lang.org
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-core
|
||||
[4]: https://web.libera.chat/#elixir
|
||||
[5]: https://libera.chat
|
||||
[6]: https://elixir-lang.org/docs.html
|
||||
[7]: CHANGELOG.md
|
||||
[8]: https://groups.google.com/group/elixir-lang-ann
|
||||
[9]: SECURITY.md
|
||||
[10]: https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date
|
||||
[3]: http://groups.google.com/group/elixir-lang-talk
|
||||
[4]: http://groups.google.com/group/elixir-lang-core
|
||||
|
||||
## License
|
||||
|
||||
"Elixir" and the Elixir logo are registered trademarks of The Elixir Team.
|
||||
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
|
||||
|
||||
Elixir source code is released under Apache License 2.0.
|
||||
Elixir source code is released under Apache 2 License with some parts under Erlang's license (EPL).
|
||||
|
||||
Check [NOTICE](NOTICE) and [LICENSE](LICENSE) files for more information.
|
||||
Check [LEGAL](LEGAL) and [LICENSE](LICENSE) files for more information.
|
||||
|
||||
+22
-28
@@ -1,49 +1,43 @@
|
||||
# Release process
|
||||
|
||||
## Shipping a new version
|
||||
## All releases
|
||||
|
||||
1. Update version in /VERSION, bin/elixir and bin/elixir.bat
|
||||
This document simply outlines the release process:
|
||||
|
||||
2. Ensure /CHANGELOG.md is updated, versioned and add the current date
|
||||
1. Ensure you are running on the oldest supported Erlang version
|
||||
|
||||
3. Update "Compatibility and Deprecations" if a new OTP version is supported
|
||||
2. Remove all `-dev` extension from versions (see below for all files)
|
||||
|
||||
4. Commit changes above with title "Release vVERSION" and push it
|
||||
3. Ensure CHANGELOG is updated and add current date
|
||||
|
||||
6. Once GitHub actions completes, generate a new tag, and push it
|
||||
4. Commit changes above with title "Release vVERSION" and generate new tag
|
||||
|
||||
7. Wait until GitHub Actions publish artifacts to the draft release
|
||||
5. Run `make clean test` to ensure all tests pass from scratch and the CI is green
|
||||
|
||||
8. Copy the relevant bits from /CHANGELOG.md to the GitHub release and publish it (link to the announcement if there is one)
|
||||
6. Ensure minimum supported Hex works with new release (instructions upcoming)
|
||||
|
||||
9. Update `_data/elixir-versions.yml` (except for RCs) in `elixir-lang/elixir-lang.github.com`
|
||||
7. Push branch and the new tag
|
||||
|
||||
## Creating a new vMAJOR.MINOR branch (before first rc)
|
||||
8. Release new docs with `make release_docs`, copy docs to `docs/stable` if appropriate, and push
|
||||
|
||||
### In the new branch
|
||||
9. Release new zip with `make release_zip`, push `Precompiled.zip` to GitHub Releases
|
||||
|
||||
1. Comment out `CANONICAL=` in /Makefile
|
||||
10. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
|
||||
11. Build and push standalone Mix with `make publish_mix` (requires AWS credentials)
|
||||
|
||||
3. Commit "Branch out vMAJOR.MINOR"
|
||||
## New vMAJOR.MINOR releases
|
||||
|
||||
### Back in main
|
||||
11. Create a new branch "vMAJOR.MINOR"
|
||||
|
||||
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
|
||||
12. Move docs generation to `docs/vOLD-MAJOR.OLD-MINOR` and copy them to `docs/stable`
|
||||
|
||||
2. Start new /CHANGELOG.md
|
||||
13. Bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
|
||||
3. Update tables in /SECURITY.md and in "Compatibility and Deprecations"
|
||||
14. `make release_docs` and push it to `elixir-lang/docs`
|
||||
|
||||
4. Commit "Start vMAJOR.MINOR+1"
|
||||
## Places where version is mentioned
|
||||
|
||||
## Changing supported Erlang/OTP versions
|
||||
|
||||
1. Update the table in Compatibility and Deprecations
|
||||
|
||||
2. Update `otp_release` checks in `/Makefile` and `/lib/elixir/src/elixir.erl`
|
||||
|
||||
3. Update relevant CI workflows in `/.github/workflows/*.yml`
|
||||
|
||||
4. Remove `otp_release` version checks that are no longer needed
|
||||
* VERSION (make sure there is no newline in this file)
|
||||
* CHANGELOG.md
|
||||
* src/elixir.app.src (not lib/elixir/src/elixir.app.src)
|
||||
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported versions
|
||||
|
||||
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
|
||||
|
||||
Elixir version | Support
|
||||
:------------- | :-----------------------------
|
||||
1.17 | Bug fixes and security patches
|
||||
1.16 | Security patches only
|
||||
1.15 | Security patches only
|
||||
1.14 | Security patches only
|
||||
1.13 | Security patches only
|
||||
|
||||
## Announcements
|
||||
|
||||
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email. Security notifications [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
|
||||
|
||||
You may also see [all releases](https://github.com/elixir-lang/elixir/releases) and [consult all disclosed vulnerabilities](https://github.com/elixir-lang/elixir/security) on GitHub.
|
||||
|
||||
## Reporting a vulnerability
|
||||
|
||||
[Please disclose security vulnerabilities privately via GitHub](https://github.com/elixir-lang/elixir/security).
|
||||
+53
-202
@@ -1,63 +1,26 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
ELIXIR_VERSION=1.17.0-rc.1
|
||||
-v Prints version and exit
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
|
||||
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
|
||||
## General options
|
||||
|
||||
-e "COMMAND" Evaluates the given command (*)
|
||||
-h, --help Prints this message (standalone)
|
||||
-r "FILE" Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in \$PATH
|
||||
-pr "FILE" Requires the given files/patterns in parallel (*)
|
||||
-pa "PATH" Prepends the given path to Erlang code path (*)
|
||||
-pz "PATH" Appends the given path to Erlang code path (*)
|
||||
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
|
||||
|
||||
--erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
--eval "COMMAND" Evaluates the given command, same as -e (*)
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--short-version Prints Elixir version (standalone)
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
Options given after the .exs file or -- are passed down to the executed code.
|
||||
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS or --erl.
|
||||
|
||||
## Distribution options
|
||||
|
||||
The following options are related to node distribution.
|
||||
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
|
||||
--name and --sname may be set to undefined so one is automatically generated.
|
||||
|
||||
## Release options
|
||||
|
||||
The following options are generally used under releases.
|
||||
|
||||
--boot "FILE" Uses the given FILE.boot to start the system
|
||||
--boot-var VAR "VALUE" Makes \$VAR available as VALUE to FILE.boot (*)
|
||||
--erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
|
||||
--pipe-to "PIPEDIR" "LOGDIR" Starts the Erlang VM as a named PIPEDIR and LOGDIR
|
||||
--vm-args "FILE" Passes the contents in file as arguments to the VM
|
||||
|
||||
--pipe-to starts Elixir detached from console (Unix-like only).
|
||||
It will attempt to create PIPEDIR and LOGDIR if they don't exist.
|
||||
See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
|
||||
|
||||
** Options marked with (*) can be given more than once.
|
||||
** Standalone options can't be combined with other options.
|
||||
USAGE
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -67,183 +30,71 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
if [ $# -eq 1 ] && [ "$1" = "--short-version" ]; then
|
||||
echo "$ELIXIR_VERSION"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Stores static Erlang arguments and --erl (which is passed as is)
|
||||
MODE="elixir"
|
||||
ERL=""
|
||||
|
||||
# Stores erl arguments preserving spaces/quotes (mimics an array)
|
||||
erl_set () {
|
||||
eval "E${E}=\$1"
|
||||
E=$((E + 1))
|
||||
}
|
||||
|
||||
# Checks if a string starts with prefix. Usage: starts_with "$STRING" "$PREFIX"
|
||||
starts_with () {
|
||||
case $1 in
|
||||
"$2"*) true;;
|
||||
*) false;;
|
||||
esac
|
||||
}
|
||||
|
||||
ERL_EXEC="erl"
|
||||
MODE="cli"
|
||||
I=1
|
||||
E=0
|
||||
LENGTH=$#
|
||||
set -- "$@" -extra
|
||||
|
||||
while [ $I -le $LENGTH ]; do
|
||||
# S counts to be shifted, C counts to be copied
|
||||
S=0
|
||||
C=0
|
||||
case "$1" in
|
||||
+elixirc)
|
||||
C=1
|
||||
;;
|
||||
while [ $I -le $# ]; do
|
||||
S=1
|
||||
eval "PEEK=\${$I}"
|
||||
case "$PEEK" in
|
||||
+iex)
|
||||
C=1
|
||||
MODE="iex"
|
||||
;;
|
||||
-v|--no-halt)
|
||||
C=1
|
||||
+elixirc)
|
||||
MODE="elixirc"
|
||||
;;
|
||||
-e|-r|-pr|-pa|-pz|--eval|--remsh|--dot-iex|--dbg)
|
||||
C=2
|
||||
-v|--compile|--no-halt)
|
||||
;;
|
||||
--rpc-eval)
|
||||
C=3
|
||||
;;
|
||||
--hidden)
|
||||
S=1
|
||||
ERL="$ERL -hidden"
|
||||
;;
|
||||
--logger-otp-reports)
|
||||
-e|-r|-pr|-pa|-pz|--remsh|--app)
|
||||
S=2
|
||||
if [ "$2" = 'true' ] || [ "$2" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports $2"
|
||||
fi
|
||||
;;
|
||||
--logger-sasl-reports)
|
||||
S=2
|
||||
if [ "$2" = 'true' ] || [ "$2" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports $2"
|
||||
fi
|
||||
;;
|
||||
--erl)
|
||||
S=2
|
||||
ERL="$ERL $2"
|
||||
--detached|--hidden)
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-`"
|
||||
;;
|
||||
--cookie)
|
||||
S=2
|
||||
erl_set "-setcookie"
|
||||
erl_set "$2"
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL -setcookie "$VAL""
|
||||
;;
|
||||
--sname|--name)
|
||||
S=2
|
||||
erl_set "$(echo "$1" | cut -c 2-)"
|
||||
erl_set "$2"
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-` "$VAL""
|
||||
;;
|
||||
--erl-config)
|
||||
S=2
|
||||
erl_set "-config"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--vm-args)
|
||||
S=2
|
||||
erl_set "-args_file"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--boot)
|
||||
S=2
|
||||
erl_set "-boot"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--boot-var)
|
||||
S=3
|
||||
erl_set "-boot_var"
|
||||
erl_set "$2"
|
||||
erl_set "$3"
|
||||
;;
|
||||
--pipe-to)
|
||||
S=3
|
||||
RUN_ERL_PIPE="$2"
|
||||
RUN_ERL_LOG="$3"
|
||||
if [ "$(starts_with "$RUN_ERL_PIPE" "-")" ]; then
|
||||
echo "--pipe-to : PIPEDIR cannot be a switch" >&2 && exit 1
|
||||
elif [ "$(starts_with "$RUN_ERL_LOG" "-")" ]; then
|
||||
echo "--pipe-to : LOGDIR cannot be a switch" >&2 && exit 1
|
||||
fi
|
||||
;;
|
||||
--werl)
|
||||
S=1
|
||||
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
|
||||
--erl)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL "$VAL""
|
||||
;;
|
||||
*)
|
||||
while [ $I -le $LENGTH ]; do
|
||||
I=$((I + 1))
|
||||
set -- "$@" "$1"
|
||||
shift
|
||||
done
|
||||
break
|
||||
;;
|
||||
esac
|
||||
|
||||
while [ $I -le $LENGTH ] && [ $C -gt 0 ]; do
|
||||
C=$((C - 1))
|
||||
I=$((I + 1))
|
||||
set -- "$@" "$1"
|
||||
shift
|
||||
done
|
||||
|
||||
I=$((I + S))
|
||||
shift $S
|
||||
done
|
||||
|
||||
I=$((E - 1))
|
||||
while [ $I -ge 0 ]; do
|
||||
eval "VAL=\$E$I"
|
||||
set -- "$VAL" "$@"
|
||||
I=$((I - 1))
|
||||
I=$(expr $I + $S)
|
||||
done
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
SCRIPT_PATH=$(dirname "$SELF")
|
||||
|
||||
if [ "$OSTYPE" = "cygwin" ]; then SCRIPT_PATH=$(cygpath -m "$SCRIPT_PATH"); fi
|
||||
if [ "$MODE" != "iex" ]; then ERL="-s elixir start_cli $ERL"; fi
|
||||
if [ "$MODE" != "iex" ]; then ERL="-noshell -s elixir start_cli $ERL"; fi
|
||||
|
||||
if [ "$OS" != "Windows_NT" ] && [ -z "$NO_COLOR" ]; then
|
||||
# Check for terminal support
|
||||
if [ "$OS" != "Windows_NT" ]; then
|
||||
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
|
||||
fi
|
||||
|
||||
# One MAY change ERTS_BIN= but you MUST NOT change
|
||||
# ERTS_BIN=$ERTS_BIN as it is handled by Elixir releases.
|
||||
ERTS_BIN=
|
||||
ERTS_BIN="$ERTS_BIN"
|
||||
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -noshell -elixir_root "$SCRIPT_PATH"/../lib -pa "$SCRIPT_PATH"/../lib/elixir/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"
|
||||
|
||||
if [ -n "$RUN_ERL_PIPE" ]; then
|
||||
ESCAPED=""
|
||||
for PART in "$@"; do
|
||||
ESCAPED="$ESCAPED $(printf '%s' "$PART" | sed 's@[^a-zA-Z0-9_/-]@\\&@g')"
|
||||
done
|
||||
mkdir -p "$RUN_ERL_PIPE"
|
||||
mkdir -p "$RUN_ERL_LOG"
|
||||
ERL_EXEC="run_erl"
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -daemon "$RUN_ERL_PIPE/" "$RUN_ERL_LOG/" "$ESCAPED"
|
||||
if [ -z "$ERL_PATH" ]; then
|
||||
if [ -f "$SCRIPT_PATH/../releases/RELEASES" ] && [ -f "$SCRIPT_PATH/erl" ]; then
|
||||
ERL_PATH="$SCRIPT_PATH"/erl
|
||||
else
|
||||
ERL_PATH=erl
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$ELIXIR_CLI_DRY_RUN" ]; then
|
||||
echo "$@"
|
||||
else
|
||||
exec "$@"
|
||||
fi
|
||||
exec "$ERL_PATH" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL -extra "$@"
|
||||
|
||||
+78
-128
@@ -1,156 +1,106 @@
|
||||
@echo off
|
||||
|
||||
set ELIXIR_VERSION=1.17.0-rc.1
|
||||
|
||||
if ""%1""=="""" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""-h"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""/h"" if ""%2""=="""" goto documentation
|
||||
if ""%1""==""/?"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""--short-version"" if ""%2""=="""" goto shortversion
|
||||
setlocal
|
||||
if "%1"=="" goto documentation
|
||||
if "%1"=="--help" goto documentation
|
||||
if "%1"=="-h" goto documentation
|
||||
if "%1"=="/h" goto documentation
|
||||
goto parseopts
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo ## General options
|
||||
echo -v Prints version and exit
|
||||
echo -e command Evaluates the given command (*)
|
||||
echo -r file Requires the given files/patterns (*)
|
||||
echo -S script Finds and executes the given script
|
||||
echo -pr file Requires the given files/patterns in parallel (*)
|
||||
echo -pa path Prepends the given path to Erlang code path (*)
|
||||
echo -pz path Appends the given path to Erlang code path (*)
|
||||
echo --app app Start the given app and its dependencies (*)
|
||||
echo --erl switches Switches to be passed down to erlang (*)
|
||||
echo --name name Makes and assigns a name to the distributed node
|
||||
echo --sname name Makes and assigns a short name to the distributed node
|
||||
echo --cookie cookie Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo.
|
||||
echo -e "COMMAND" Evaluates the given command (*)
|
||||
echo -h, --help Prints this message (standalone)
|
||||
echo -r "FILE" Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in $PATH
|
||||
echo -pr "FILE" Requires the given files/patterns in parallel (*)
|
||||
echo -pa "PATH" Prepends the given path to Erlang code path (*)
|
||||
echo -pz "PATH" Appends the given path to Erlang code path (*)
|
||||
echo -v, --version Prints Erlang/OTP and Elixir versions (standalone)
|
||||
echo.
|
||||
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
echo --eval "COMMAND" Evaluates the given command, same as -e (*)
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --short-version Prints Elixir version (standalone)
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo Options given after the .exs file or -- are passed down to the executed code.
|
||||
echo Options can be passed to the Erlang runtime using $ELIXIR_ERL_OPTIONS or --erl.
|
||||
echo.
|
||||
echo ## Distribution options
|
||||
echo.
|
||||
echo The following options are related to node distribution.
|
||||
echo.
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo.
|
||||
echo --name and --sname may be set to undefined so one is automatically generated.
|
||||
echo.
|
||||
echo ## Release options
|
||||
echo.
|
||||
echo The following options are generally used under releases.
|
||||
echo.
|
||||
echo --boot "FILE" Uses the given FILE.boot to start the system
|
||||
echo --boot-var VAR "VALUE" Makes $VAR available as VALUE to FILE.boot (*)
|
||||
echo --erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
|
||||
echo --vm-args "FILE" Passes the contents in file as arguments to the VM
|
||||
echo.
|
||||
echo --pipe-to is not supported on Windows. If set, Elixir won't boot.
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once.
|
||||
echo ** Standalone options can't be combined with other options.
|
||||
goto end
|
||||
|
||||
:shortversion
|
||||
echo %ELIXIR_VERSION%
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:parseopts
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
rem Parameters for Erlang
|
||||
set parsErlang=
|
||||
|
||||
rem Make sure we keep a copy of all parameters
|
||||
set allPars=%*
|
||||
|
||||
rem Get the original path name from the batch file
|
||||
set originPath=%~dp0
|
||||
|
||||
rem Optional parameters before the "-extra" parameter
|
||||
set beforeExtra=
|
||||
|
||||
rem Option which determines whether the loop is over
|
||||
set endLoop=0
|
||||
rem Flag which determines whether or not to use werl vs erl
|
||||
set useWerl=0
|
||||
|
||||
rem Designates the path to the current script
|
||||
set SCRIPT_PATH=%~dp0
|
||||
|
||||
rem Designates the path to the ERTS system
|
||||
set ERTS_BIN=
|
||||
set ERTS_BIN=!ERTS_BIN!
|
||||
rem Designates which mode / Elixir component to run as
|
||||
set runMode="elixir"
|
||||
|
||||
rem Recursive loop called for each parameter that parses the cmd line parameters
|
||||
:startloop
|
||||
set "par=%~1"
|
||||
if "!par!"=="" (
|
||||
rem skip if no parameter
|
||||
goto run
|
||||
)
|
||||
set par="%1"
|
||||
shift
|
||||
set par="!par:"=\"!"
|
||||
rem ******* EXECUTION OPTIONS **********************
|
||||
if !par!=="--werl" (set useWerl=1 && goto startloop)
|
||||
if !par!=="+iex" (set useIEx=1 && goto startloop)
|
||||
if !par!=="+elixirc" (goto startloop)
|
||||
rem ******* ELIXIR PARAMETERS **********************
|
||||
if ""==!par:-e=! (shift && goto startloop)
|
||||
if ""==!par:--eval=! (shift && goto startloop)
|
||||
if ""==!par:--rpc-eval=! (shift && shift && goto startloop)
|
||||
if ""==!par:-r=! (shift && goto startloop)
|
||||
if ""==!par:-pr=! (shift && goto startloop)
|
||||
if ""==!par:-pa=! (shift && goto startloop)
|
||||
if ""==!par:-pz=! (shift && goto startloop)
|
||||
if ""==!par:-v=! (goto startloop)
|
||||
if ""==!par:--version=! (goto startloop)
|
||||
if ""==!par:--no-halt=! (goto startloop)
|
||||
if ""==!par:--remsh=! (shift && goto startloop)
|
||||
if ""==!par:--dot-iex=! (shift && goto startloop)
|
||||
if ""==!par:--dbg=! (shift && goto startloop)
|
||||
rem ******* ERLANG PARAMETERS **********************
|
||||
if ""==!par:--boot=! (set "parsErlang=!parsErlang! -boot "%~1"" && shift && goto startloop)
|
||||
if ""==!par:--boot-var=! (set "parsErlang=!parsErlang! -boot_var "%~1" "%~2"" && shift && shift && goto startloop)
|
||||
if ""==!par:--cookie=! (set "parsErlang=!parsErlang! -setcookie "%~1"" && shift && goto startloop)
|
||||
if ""==!par:--hidden=! (set "parsErlang=!parsErlang! -hidden" && goto startloop)
|
||||
if ""==!par:--erl-config=! (set "parsErlang=!parsErlang! -config "%~1"" && shift && goto startloop)
|
||||
if ""==!par:--logger-otp-reports=! (set "parsErlang=!parsErlang! -logger handle_otp_reports %1" && shift && goto startloop)
|
||||
if ""==!par:--logger-sasl-reports=! (set "parsErlang=!parsErlang! -logger handle_sasl_reports %1" && shift && goto startloop)
|
||||
if ""==!par:--name=! (set "parsErlang=!parsErlang! -name "%~1"" && shift && goto startloop)
|
||||
if ""==!par:--sname=! (set "parsErlang=!parsErlang! -sname "%~1"" && shift && goto startloop)
|
||||
if ""==!par:--vm-args=! (set "parsErlang=!parsErlang! -args_file "%~1"" && shift && goto startloop)
|
||||
if ""==!par:--erl=! (set "beforeExtra=!beforeExtra! %~1" && shift && goto startloop)
|
||||
if ""==!par:--pipe-to=! (echo --pipe-to : Option is not supported on Windows && goto end)
|
||||
|
||||
:run
|
||||
setlocal disabledelayedexpansion
|
||||
reg query HKCU\Console /v VirtualTerminalLevel 2>nul | findstr /e "0x1" >nul 2>nul
|
||||
if %errorlevel% == 0 (
|
||||
set beforeExtra=-elixir ansi_enabled true %beforeExtra%
|
||||
if "%par%"=="" (
|
||||
rem if no parameters defined
|
||||
goto :expand_erl_libs
|
||||
)
|
||||
if not defined useIEx (
|
||||
if "%par%"=="""" (
|
||||
rem if no parameters defined - special case for parameter that is already quoted
|
||||
goto :expand_erl_libs
|
||||
)
|
||||
rem ******* EXECUTION OPTIONS **********************
|
||||
IF "%par%"==""--werl"" (Set useWerl=1)
|
||||
IF "%par%"==""+iex"" (Set runMode="iex")
|
||||
rem ******* elixir parameters **********************
|
||||
rem Note: we don't have to do anything with options that don't take an argument
|
||||
IF """"=="%par:-e=%" (shift)
|
||||
IF """"=="%par:-r=%" (shift)
|
||||
IF """"=="%par:-pr=%" (shift)
|
||||
IF """"=="%par:-pa=%" (shift)
|
||||
IF """"=="%par:-pz=%" (shift)
|
||||
IF """"=="%par:--app=%" (shift)
|
||||
IF """"=="%par:--remsh=%" (shift)
|
||||
rem ******* ERLANG PARAMETERS **********************
|
||||
IF """"=="%par:--detached=%" (Set parsErlang=%parsErlang% -detached)
|
||||
IF """"=="%par:--hidden=%" (Set parsErlang=%parsErlang% -hidden)
|
||||
IF """"=="%par:--cookie=%" (Set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
IF """"=="%par:--sname=%" (Set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
IF """"=="%par:--name=%" (Set parsErlang=%parsErlang% -name %1 && shift)
|
||||
IF """"=="%par:--erl=%" (Set beforeExtra=%beforeExtra% %~1 && shift)
|
||||
goto:startloop
|
||||
|
||||
rem ******* assume all pre-params are parsed ********************
|
||||
:expand_erl_libs
|
||||
rem ******* expand all ebin paths as Windows does not support the ..\*\ebin wildcard ********************
|
||||
SETLOCAL enabledelayedexpansion
|
||||
set ext_libs=
|
||||
for /d %%d in ("%originPath%..\lib\*.") do (
|
||||
set ext_libs=!ext_libs! -pa "%%~fd\ebin"
|
||||
)
|
||||
SETLOCAL disabledelayedexpansion
|
||||
:run
|
||||
IF NOT %runMode% == "iex" (
|
||||
set beforeExtra=-s elixir start_cli %beforeExtra%
|
||||
)
|
||||
|
||||
set beforeExtra=-noshell -elixir_root "%SCRIPT_PATH%..\lib" -pa "%SCRIPT_PATH%..\lib\elixir\ebin" %beforeExtra%
|
||||
|
||||
if defined ELIXIR_CLI_DRY_RUN (
|
||||
if defined useWerl (
|
||||
echo start "" "%ERTS_BIN%werl.exe" %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) else (
|
||||
echo "%ERTS_BIN%erl.exe" %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
) else (
|
||||
if defined useWerl (
|
||||
start "" "%ERTS_BIN%werl.exe" %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) else (
|
||||
"%ERTS_BIN%erl.exe" %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
IF %useWerl% EQU 1 (
|
||||
start werl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) ELSE (
|
||||
erl.exe %ext_libs% -noshell %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
exit /B %ERRORLEVEL%
|
||||
:end
|
||||
endlocal
|
||||
|
||||
+11
-19
@@ -1,25 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
|
||||
echo "Usage: `basename $0` [elixir switches] [compiler switches] [.ex files]
|
||||
|
||||
-h, --help Prints this message and exits
|
||||
-o The directory to output compiled files
|
||||
-v, --version Prints Elixir version and exits (standalone)
|
||||
-o The directory to output compiled files
|
||||
--no-docs Do not attach documentation to compiled modules
|
||||
--no-debug-info Do not attach debug info to compiled modules
|
||||
--ignore-module-conflict
|
||||
--warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
--verbose Print informational messages.
|
||||
|
||||
--ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
--no-debug-info Does not attach debug info to compiled modules
|
||||
--no-docs Does not attach documentation to compiled modules
|
||||
--profile time Profile the time to compile modules
|
||||
--verbose Prints compilation status
|
||||
--warnings-as-errors Treats warnings as errors and return non-zero exit status
|
||||
|
||||
Options given after -- are passed down to the executed code.
|
||||
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS.
|
||||
Options can be passed to the Erlang compiler using \$ERL_COMPILER_OPTIONS.
|
||||
USAGE
|
||||
** Options given after -- are passed down to the executed code
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -29,7 +21,7 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
+12
-23
@@ -1,12 +1,10 @@
|
||||
@echo off
|
||||
setlocal
|
||||
set argc=0
|
||||
for %%A in (%*) do (
|
||||
if /I "%%A"=="--help" goto documentation
|
||||
if /I "%%A"=="-h" goto documentation
|
||||
if /I "%%A"=="/h" goto documentation
|
||||
if "%%A"=="/?" goto documentation
|
||||
set /A argc+=1
|
||||
if "%%A"=="--help" goto documentation
|
||||
if "%%A"=="-h" goto documentation
|
||||
if "%%A"=="/h" goto documentation
|
||||
set /A argc+=1
|
||||
)
|
||||
if %argc%==0 goto documentation
|
||||
goto run
|
||||
@@ -14,24 +12,15 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
|
||||
echo.
|
||||
echo -h, --help Prints this message and exits
|
||||
echo -o The directory to output compiled files
|
||||
echo -v, --version Prints Elixir version and exits (standalone)
|
||||
echo.
|
||||
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
echo --no-debug-info Does not attach debug info to compiled modules
|
||||
echo --no-docs Does not attach documentation to compiled modules
|
||||
echo --profile time Profile the time to compile modules
|
||||
echo --verbose Prints compilation status
|
||||
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit status
|
||||
echo -o The directory to output compiled files
|
||||
echo --no-docs Do not attach documentation to compiled modules
|
||||
echo --no-debug-info Do not attach debug info to compiled modules
|
||||
echo --ignore-module-conflict
|
||||
echo --warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
echo --verbose Print informational messages.
|
||||
echo.
|
||||
echo ** Options given after -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
echo ** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS
|
||||
goto end
|
||||
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
echo ** Options can be passed to the erlang compiler using ERL_COMPILER_OPTIONS >&2
|
||||
:run
|
||||
call "%~dp0\elixir.bat" +elixirc %*
|
||||
|
||||
:end
|
||||
endlocal
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
-v Prints version
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--remsh \"name\" Connects to a node using a remote shell
|
||||
--dot-iex \"path\" Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
|
||||
The following options are exclusive to IEx:
|
||||
|
||||
--dbg pry Sets the backend for Kernel.dbg/2 to IEx.pry/0
|
||||
--dot-iex "FILE" Evaluates FILE, line by line, to set up IEx' environment.
|
||||
Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
|
||||
If FILE is empty, then no file will be loaded.
|
||||
--remsh NAME Connects to a node using a remote shell.
|
||||
|
||||
It accepts all other options listed by "elixir --help".
|
||||
USAGE
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
** Options can be passed to the VM using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -24,10 +32,12 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
SCRIPT_PATH=$(dirname "$SELF")
|
||||
exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user elixir" +iex "$@"
|
||||
|
||||
if [ "$OS" = "Windows_NT" ]; then NOSHELL="-noshell "; fi
|
||||
exec "$SCRIPT_PATH"/elixir --no-halt --erl "$NOSHELL -user Elixir.IEx.CLI" +iex "$@"
|
||||
|
||||
+1
-28
@@ -1,29 +1,2 @@
|
||||
@echo off
|
||||
setlocal
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
if /I ""%1""==""/h"" goto documentation
|
||||
if ""%1""==""/?"" goto documentation
|
||||
goto run
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo The following options are exclusive to IEx:
|
||||
echo.
|
||||
echo --dbg pry Sets the backend for Kernel.dbg/2 to IEx.pry/0
|
||||
echo --dot-iex "FILE" Evaluates FILE, line by line, to set up IEx' environment.
|
||||
echo Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
|
||||
echo If FILE is empty, then no file will be loaded.
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo Set the IEX_WITH_WERL environment variable to always use werl.
|
||||
echo It accepts all other options listed by "elixir --help".
|
||||
goto end
|
||||
|
||||
:run
|
||||
if defined IEX_WITH_WERL (set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
call "%~dp0\elixir.bat" --no-halt --erl "-user elixir" +iex %__ELIXIR_IEX_FLAGS% %*
|
||||
:end
|
||||
endlocal
|
||||
call "%~dp0\elixir.bat" +iex --erl "-user Elixir.IEx.CLI" --no-halt %*
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
#!/usr/bin/env elixir
|
||||
Mix.CLI.main()
|
||||
# Reminder: apply any changes in this file to bin\mix.bat
|
||||
Mix.start
|
||||
Mix.CLI.main
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
@echo off
|
||||
call "%~dp0\elixir.bat" "%~dp0\mix" %*
|
||||
call "%~dp0\elixir.bat" -e Mix.start -e Mix.CLI.main %*
|
||||
|
||||
+86
-217
@@ -1,84 +1,47 @@
|
||||
defmodule EEx.SyntaxError do
|
||||
defexception [:file, :line, :column, :snippet, message: "syntax error"]
|
||||
defexception [:message, :file, :line]
|
||||
|
||||
@impl true
|
||||
def message(exception) do
|
||||
%{file: file, line: line, column: column, message: message, snippet: snippet} = exception
|
||||
|
||||
Exception.format_file_line_column(file && Path.relative_to_cwd(file), line, column, " ") <>
|
||||
message <> (snippet || "")
|
||||
"#{exception.file}:#{exception.line}: #{exception.message}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule EEx do
|
||||
@moduledoc ~S"""
|
||||
EEx stands for Embedded Elixir.
|
||||
EEx stands for Embedded Elixir. It allows you to embed
|
||||
Elixir code inside a string in a robust way:
|
||||
|
||||
Embedded Elixir allows you to embed Elixir code inside a string
|
||||
in a robust way.
|
||||
|
||||
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
|
||||
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
"foo baz"
|
||||
|
||||
This module provides three main APIs for you to use:
|
||||
## API
|
||||
|
||||
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
|
||||
This module provides 3 main APIs for you to use:
|
||||
|
||||
1. Evaluate a string (`eval_string`) or a file (`eval_file`)
|
||||
directly. This is the simplest API to use but also the
|
||||
slowest, since the code is evaluated at runtime and not precompiled.
|
||||
slowest, since the code is evaluated and not compiled before.
|
||||
|
||||
2. Define a function from a string (`function_from_string/5`)
|
||||
or a file (`function_from_file/5`). This allows you to embed
|
||||
2. Define a function from a string (`function_from_string`)
|
||||
or a file (`function_from_file`). This allows you to embed
|
||||
the template as a function inside a module which will then
|
||||
be compiled. This is the preferred API if you have access
|
||||
to the template at compilation time.
|
||||
|
||||
3. Compile a string (`compile_string/2`) or a file (`compile_file/2`)
|
||||
3. Compile a string (`compile_string`) or a file (`compile_file`)
|
||||
into Elixir syntax tree. This is the API used by both functions
|
||||
above and is available to you if you want to provide your own
|
||||
ways of handling the compiled template.
|
||||
|
||||
The APIs above support several options, documented below. You may
|
||||
also pass an engine which customizes how the EEx code is compiled.
|
||||
|
||||
## Options
|
||||
|
||||
All functions in this module, unless otherwise noted, accept EEx-related
|
||||
options. They are:
|
||||
All functions in this module accepts EEx-related options.
|
||||
They are:
|
||||
|
||||
* `:line` - the line to be used as the template start. Defaults to 1.
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to `"nofile"` when compiling from a string.
|
||||
|
||||
* `:line` - the line to be used as the template start. Defaults to `1`.
|
||||
|
||||
* `:indentation` - (since v1.11.0) an integer added to the column after every
|
||||
new line. Defaults to `0`.
|
||||
|
||||
* `:engine` - the EEx engine to be used for compilation. Defaults to `EEx.SmartEngine`.
|
||||
|
||||
* `:trim` - if `true`, trims whitespace left and right of quotation as
|
||||
long as at least one newline is present. All subsequent newlines and
|
||||
spaces are removed but one newline is retained. Defaults to `false`.
|
||||
|
||||
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code
|
||||
that is generated. See `Code.string_to_quoted/2` for available options.
|
||||
Note that the options `:file`, `:line` and `:column` are ignored if
|
||||
passed in. Defaults to `Code.get_compiler_option(:parser_options)`
|
||||
(which defaults to `[]` if not set).
|
||||
|
||||
## Tags
|
||||
|
||||
EEx supports multiple tags, declared below:
|
||||
|
||||
<% Elixir expression: executes code but discards output %>
|
||||
<%= Elixir expression: executes code and prints result %>
|
||||
<%% EEx quotation: returns the contents inside the tag as is %>
|
||||
<%!-- Comments: they are discarded from source --%>
|
||||
|
||||
EEx supports additional tags, that may be used by some engines,
|
||||
but they do not have a meaning by default:
|
||||
|
||||
<%| ... %>
|
||||
<%/ ... %>
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -88,62 +51,73 @@ defmodule EEx do
|
||||
By default, `EEx` uses the `EEx.SmartEngine` that provides some
|
||||
conveniences on top of the simple `EEx.Engine`.
|
||||
|
||||
### `EEx.SmartEngine`
|
||||
### Tags
|
||||
|
||||
The smart engine uses EEx default rules and adds the `@` construct
|
||||
for reading template assigns:
|
||||
`EEx.SmartEngine` supports the following tags:
|
||||
|
||||
iex> EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
|
||||
<% Elixir expression - inline with output %>
|
||||
<%= Elixir expression - replace with result %>
|
||||
<%% EEx quotation - returns the contents inside %>
|
||||
<%# Comments - they are discarded from source %>
|
||||
|
||||
All expressions that output something to the template
|
||||
**must** use the equals sign (`=`). Since everything in
|
||||
Elixir is a macro, there are no exceptions for this rule.
|
||||
For example, while some template languages would special-
|
||||
case `if` clauses, they are treated the same in EEx and
|
||||
also require `=` in order to have their result printed:
|
||||
|
||||
<%= if true do %>
|
||||
It is obviously true
|
||||
<% else %>
|
||||
This will never appear
|
||||
<% end %>
|
||||
|
||||
Notice that different engines may have different rules
|
||||
for each tag. Other tags may be added in future versions.
|
||||
|
||||
### Macros
|
||||
|
||||
`EEx.SmartEngine` also adds some macros to your template.
|
||||
An example is the `@` macro which allows easy data access
|
||||
in a template:
|
||||
|
||||
iex> EEx.eval_string "<%= @foo %>", assigns: [foo: 1]
|
||||
"1"
|
||||
|
||||
In other words, `<%= @foo %>` translates to:
|
||||
In other words, `<%= @foo %>` is simply translated to:
|
||||
|
||||
<%= {:ok, v} = Access.fetch(assigns, :foo); v %>
|
||||
<%= Dict.get assigns, :foo %>
|
||||
|
||||
The `assigns` extension is useful when the number of variables
|
||||
The assigns extension is useful when the number of variables
|
||||
required by the template is not specified at compilation time.
|
||||
"""
|
||||
|
||||
@type line :: non_neg_integer
|
||||
@type column :: non_neg_integer
|
||||
@type marker :: [?=] | [?/] | [?|] | []
|
||||
@type metadata :: %{column: column, line: line}
|
||||
@type token ::
|
||||
{:comment, charlist, metadata}
|
||||
| {:text, charlist, metadata}
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, marker, charlist, metadata}
|
||||
| {:eof, metadata}
|
||||
|
||||
@doc """
|
||||
Generates a function definition from the given string.
|
||||
Generates a function definition from the string.
|
||||
|
||||
The first argument is the kind of the generated function (`:def` or `:defp`).
|
||||
The `name` argument is the name that the generated function will have.
|
||||
`template` is the string containing the EEx template. `args` is a list of arguments
|
||||
that the generated function will accept. They will be available inside the EEx
|
||||
template.
|
||||
|
||||
The supported `options` are described [in the module docs](#module-options).
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> defmodule Sample do
|
||||
...> require EEx
|
||||
...> EEx.function_from_string(:def, :sample, "<%= a + b %>", [:a, :b])
|
||||
...> EEx.function_from_string :def, :sample, "<%= a + b %>", [:a, :b]
|
||||
...> end
|
||||
iex> Sample.sample(1, 2)
|
||||
"3"
|
||||
|
||||
"""
|
||||
defmacro function_from_string(kind, name, template, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
info = Keyword.merge([file: __ENV__.file, line: __ENV__.line], options)
|
||||
args = Enum.map(args, fn arg -> {arg, [line: info[:line]], nil} end)
|
||||
compiled = EEx.compile_string(template, info)
|
||||
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding do
|
||||
info = Keyword.merge [file: __ENV__.file, line: __ENV__.line], options
|
||||
args = Enum.map args, fn arg -> {arg, [line: info[:line]], nil} end
|
||||
compiled = EEx.compile_string(source, info)
|
||||
|
||||
case kind do
|
||||
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
|
||||
:defp -> defp unquote(name)(unquote_splicing(args)), do: unquote(compiled)
|
||||
:def -> def(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
:defp -> defp(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -151,17 +125,12 @@ defmodule EEx do
|
||||
@doc """
|
||||
Generates a function definition from the file contents.
|
||||
|
||||
The first argument is the kind of the generated function (`:def` or `:defp`).
|
||||
The `name` argument is the name that the generated function will have.
|
||||
`file` is the path to the EEx template file. `args` is a list of arguments
|
||||
that the generated function will accept. They will be available inside the EEx
|
||||
template.
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
This function is useful in case you have templates but
|
||||
you want to precompile inside a module for speed.
|
||||
|
||||
The supported `options` are described [in the module docs](#module-options).
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
@@ -170,177 +139,77 @@ defmodule EEx do
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file(:def, :sample, "sample.eex", [:a, :b])
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:a, :b]
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(1, 2)
|
||||
#=> "3"
|
||||
Sample.sample(1, 2) #=> "3"
|
||||
|
||||
"""
|
||||
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
info = Keyword.merge([file: IO.chardata_to_string(file), line: 1], options)
|
||||
args = Enum.map(args, fn arg -> {arg, [line: 1], nil} end)
|
||||
quote bind_quoted: binding do
|
||||
info = Keyword.merge options, [file: file, line: 1]
|
||||
args = Enum.map args, fn arg -> {arg, [line: 1], nil} end
|
||||
compiled = EEx.compile_file(file, info)
|
||||
|
||||
@external_resource file
|
||||
@file file
|
||||
case kind do
|
||||
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
|
||||
:defp -> defp unquote(name)(unquote_splicing(args)), do: unquote(compiled)
|
||||
:def -> def(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
:defp -> defp(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and generates a quoted expression
|
||||
Get a string `source` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
|
||||
This is useful if you want to compile a EEx template into code and inject
|
||||
that code somewhere or evaluate it at runtime.
|
||||
|
||||
The generated quoted code will use variables defined in the template that
|
||||
will be taken from the context where the code is evaluated. If you
|
||||
have a template such as `<%= a + b %>`, then the returned quoted code
|
||||
will use the `a` and `b` variables in the context where it's evaluated. See
|
||||
examples below.
|
||||
|
||||
The supported `options` are described [in the module docs](#module-options).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> quoted = EEx.compile_string("<%= a + b %>")
|
||||
iex> {result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
|
||||
iex> result
|
||||
"3"
|
||||
|
||||
"""
|
||||
@spec compile_string(String.t(), keyword) :: Macro.t()
|
||||
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
|
||||
case tokenize(source, options) do
|
||||
{:ok, tokens} ->
|
||||
EEx.Compiler.compile(tokens, source, options)
|
||||
|
||||
{:error, message, %{column: column, line: line}} ->
|
||||
file = options[:file] || "nofile"
|
||||
raise EEx.SyntaxError, file: file, line: line, column: column, message: message
|
||||
end
|
||||
def compile_string(source, options \\ []) do
|
||||
EEx.Compiler.compile(source, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and generates a quoted expression
|
||||
Get a `filename` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
|
||||
This is useful if you want to compile a EEx template into code and inject
|
||||
that code somewhere or evaluate it at runtime.
|
||||
|
||||
The generated quoted code will use variables defined in the template that
|
||||
will be taken from the context where the code is evaluated. If you
|
||||
have a template such as `<%= a + b %>`, then the returned quoted code
|
||||
will use the `a` and `b` variables in the context where it's evaluated. See
|
||||
examples below.
|
||||
|
||||
The supported `options` are described [in the module docs](#module-options).
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
<%= a + b %>
|
||||
|
||||
# In code:
|
||||
quoted = EEx.compile_file("sample.eex")
|
||||
{result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
|
||||
result
|
||||
#=> "3"
|
||||
|
||||
"""
|
||||
@spec compile_file(Path.t(), keyword) :: Macro.t()
|
||||
def compile_file(filename, options \\ []) when is_list(options) do
|
||||
filename = IO.chardata_to_string(filename)
|
||||
options = Keyword.merge([file: filename, line: 1], options)
|
||||
def compile_file(filename, options \\ []) do
|
||||
options = Keyword.merge options, [file: filename, line: 1]
|
||||
compile_string(File.read!(filename), options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and evaluate the values using the `bindings`.
|
||||
|
||||
The supported `options` are described [in the module docs](#module-options).
|
||||
Get a string `source` and evaluate the values using the `bindings`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
|
||||
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
"foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_string(String.t(), keyword, keyword) :: String.t()
|
||||
def eval_string(source, bindings \\ [], options \\ [])
|
||||
when is_binary(source) and is_list(bindings) and is_list(options) do
|
||||
def eval_string(source, bindings \\ [], options \\ []) do
|
||||
compiled = compile_string(source, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and evaluate the values using the `bindings`.
|
||||
|
||||
The supported `options` are described [in the module docs](#module-options).
|
||||
Get a `filename` and evaluate the values using the `bindings`.
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
# sample.ex
|
||||
foo <%= bar %>
|
||||
|
||||
# IEx
|
||||
EEx.eval_file("sample.eex", bar: "baz")
|
||||
#=> "foo baz"
|
||||
# iex
|
||||
EEx.eval_file "sample.ex", [bar: "baz"] #=> "foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_file(Path.t(), keyword, keyword) :: String.t()
|
||||
def eval_file(filename, bindings \\ [], options \\ [])
|
||||
when is_list(bindings) and is_list(options) do
|
||||
filename = IO.chardata_to_string(filename)
|
||||
options = Keyword.put_new(options, :file, filename)
|
||||
def eval_file(filename, bindings \\ [], options \\ []) do
|
||||
options = Keyword.put options, :file, filename
|
||||
compiled = compile_file(filename, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Tokenize the given contents according to the given options.
|
||||
|
||||
## Options
|
||||
|
||||
* `:line` - An integer to start as line. Default is 1.
|
||||
* `:column` - An integer to start as column. Default is 1.
|
||||
* `:indentation` - An integer that indicates the indentation. Default is 0.
|
||||
* `:trim` - Tells the tokenizer to either trim the content or not. Default is false.
|
||||
* `:file` - Can be either a file or a string "nofile".
|
||||
|
||||
## Examples
|
||||
|
||||
iex> EEx.tokenize(~c"foo", line: 1, column: 1)
|
||||
{:ok, [{:text, ~c"foo", %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
|
||||
|
||||
## Result
|
||||
|
||||
It returns `{:ok, [token]}` where a token is one of:
|
||||
|
||||
* `{:text, content, %{column: column, line: line}}`
|
||||
* `{:expr, marker, content, %{column: column, line: line}}`
|
||||
* `{:start_expr, marker, content, %{column: column, line: line}}`
|
||||
* `{:middle_expr, marker, content, %{column: column, line: line}}`
|
||||
* `{:end_expr, marker, content, %{column: column, line: line}}`
|
||||
* `{:eof, %{column: column, line: line}}`
|
||||
|
||||
Or `{:error, message, %{column: column, line: line}}` in case of errors.
|
||||
Note new tokens may be added in the future.
|
||||
"""
|
||||
@doc since: "1.14.0"
|
||||
@spec tokenize([char()] | String.t(), opts :: keyword) ::
|
||||
{:ok, [token()]} | {:error, String.t(), metadata()}
|
||||
def tokenize(contents, opts \\ []) do
|
||||
EEx.Compiler.tokenize(contents, opts)
|
||||
end
|
||||
|
||||
### Helpers
|
||||
|
||||
defp do_eval(compiled, bindings, options) do
|
||||
|
||||
+53
-455
@@ -3,466 +3,103 @@ defmodule EEx.Compiler do
|
||||
|
||||
# When changing this setting, don't forget to update the docs for EEx
|
||||
@default_engine EEx.SmartEngine
|
||||
@h_spaces [?\s, ?\t]
|
||||
@all_spaces [?\s, ?\t, ?\n, ?\r]
|
||||
|
||||
@doc """
|
||||
Tokenize EEx contents.
|
||||
"""
|
||||
def tokenize(contents, opts) when is_binary(contents) do
|
||||
tokenize(String.to_charlist(contents), contents, opts)
|
||||
end
|
||||
|
||||
def tokenize(contents, opts) when is_list(contents) do
|
||||
tokenize(contents, List.to_string(contents), opts)
|
||||
end
|
||||
|
||||
def tokenize(contents, source, opts) when is_list(contents) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
trim = opts[:trim] || false
|
||||
indentation = opts[:indentation] || 0
|
||||
column = indentation + (opts[:column] || 1)
|
||||
|
||||
state = %{trim: trim, indentation: indentation, file: file, source: source}
|
||||
|
||||
{contents, line, column} =
|
||||
(trim && trim_init(contents, line, column, state)) || {contents, line, column}
|
||||
|
||||
tokenize(contents, line, column, state, [{line, column}], [])
|
||||
end
|
||||
|
||||
defp tokenize(~c"<%%" ++ t, line, column, state, buffer, acc) do
|
||||
tokenize(t, line, column + 3, state, [?%, ?< | buffer], acc)
|
||||
end
|
||||
|
||||
defp tokenize(~c"<%!--" ++ t, line, column, state, buffer, acc) do
|
||||
case comment(t, line, column + 5, state, []) do
|
||||
{:error, message} ->
|
||||
meta = %{line: line, column: column}
|
||||
{:error, message <> code_snippet(state.source, state.indentation, meta), meta}
|
||||
|
||||
{:ok, new_line, new_column, rest, comments} ->
|
||||
token = {:comment, Enum.reverse(comments), %{line: line, column: column}}
|
||||
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, &[token | &1])
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Deprecate this on Elixir v1.18
|
||||
defp tokenize(~c"<%#" ++ t, line, column, state, buffer, acc) do
|
||||
case expr(t, line, column + 3, state, []) do
|
||||
{:error, message} ->
|
||||
{:error, message, %{line: line, column: column}}
|
||||
|
||||
{:ok, _, new_line, new_column, rest} ->
|
||||
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, & &1)
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize(~c"<%" ++ t, line, column, state, buffer, acc) do
|
||||
{marker, t} = retrieve_marker(t)
|
||||
marker_length = length(marker)
|
||||
|
||||
case expr(t, line, column + 2 + marker_length, state, []) do
|
||||
{:error, message} ->
|
||||
meta = %{line: line, column: column}
|
||||
{:error, message <> code_snippet(state.source, state.indentation, meta), meta}
|
||||
|
||||
{:ok, expr, new_line, new_column, rest} ->
|
||||
{key, expr} =
|
||||
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, _line, _column, _warnings, rev_tokens, []} ->
|
||||
# We ignore warnings because the code will be tokenized
|
||||
# again later with the right line+column info
|
||||
token_key(rev_tokens, expr)
|
||||
|
||||
{:error, _, _, _, _} ->
|
||||
{:expr, expr}
|
||||
end
|
||||
|
||||
marker =
|
||||
if key in [:middle_expr, :end_expr] and marker != ~c"" do
|
||||
message =
|
||||
"unexpected beginning of EEx tag \"<%#{marker}\" on \"<%#{marker}#{expr}%>\", " <>
|
||||
"please remove \"#{marker}\""
|
||||
|
||||
:elixir_errors.erl_warn({line, column}, state.file, message)
|
||||
~c""
|
||||
else
|
||||
marker
|
||||
end
|
||||
|
||||
token = {key, marker, expr, %{line: line, column: column}}
|
||||
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, &[token | &1])
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize([?\n | t], line, _column, state, buffer, acc) do
|
||||
tokenize(t, line + 1, state.indentation + 1, state, [?\n | buffer], acc)
|
||||
end
|
||||
|
||||
defp tokenize([h | t], line, column, state, buffer, acc) do
|
||||
tokenize(t, line, column + 1, state, [h | buffer], acc)
|
||||
end
|
||||
|
||||
defp tokenize([], line, column, _state, buffer, acc) do
|
||||
eof = {:eof, %{line: line, column: column}}
|
||||
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
|
||||
end
|
||||
|
||||
defp trim_and_tokenize(rest, line, column, state, buffer, acc, fun) do
|
||||
{rest, line, column, buffer} = trim_if_needed(rest, line, column, state, buffer)
|
||||
|
||||
acc = tokenize_text(buffer, acc)
|
||||
tokenize(rest, line, column, state, [{line, column}], fun.(acc))
|
||||
end
|
||||
|
||||
# Retrieve marker for <%
|
||||
|
||||
defp retrieve_marker([marker | t]) when marker in [?=, ?/, ?|] do
|
||||
{[marker], t}
|
||||
end
|
||||
|
||||
defp retrieve_marker(t) do
|
||||
{~c"", t}
|
||||
end
|
||||
|
||||
# Tokenize a multi-line comment until we find --%>
|
||||
|
||||
defp comment([?-, ?-, ?%, ?> | t], line, column, _state, buffer) do
|
||||
{:ok, line, column + 4, t, buffer}
|
||||
end
|
||||
|
||||
defp comment([?\n | t], line, _column, state, buffer) do
|
||||
comment(t, line + 1, state.indentation + 1, state, [?\n | buffer])
|
||||
end
|
||||
|
||||
defp comment([head | t], line, column, state, buffer) do
|
||||
comment(t, line, column + 1, state, [head | buffer])
|
||||
end
|
||||
|
||||
defp comment([], _line, _column, _state, _buffer) do
|
||||
{:error, "expected closing '--%>' for EEx expression"}
|
||||
end
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
|
||||
defp expr([?%, ?> | t], line, column, _state, buffer) do
|
||||
{:ok, Enum.reverse(buffer), line, column + 2, t}
|
||||
end
|
||||
|
||||
defp expr([?\n | t], line, _column, state, buffer) do
|
||||
expr(t, line + 1, state.indentation + 1, state, [?\n | buffer])
|
||||
end
|
||||
|
||||
defp expr([h | t], line, column, state, buffer) do
|
||||
expr(t, line, column + 1, state, [h | buffer])
|
||||
end
|
||||
|
||||
defp expr([], _line, _column, _state, _buffer) do
|
||||
{:error, "expected closing '%>' for EEx expression"}
|
||||
end
|
||||
|
||||
# Receives tokens and check if it is a start, middle or an end token.
|
||||
defp token_key(rev_tokens, expr) do
|
||||
case {Enum.reverse(rev_tokens), drop_eol(rev_tokens)} do
|
||||
{[{:end, _} | _], [{:do, _} | _]} ->
|
||||
{:middle_expr, expr}
|
||||
|
||||
{_, [{:do, _} | _]} ->
|
||||
{:start_expr, maybe_append_space(expr)}
|
||||
|
||||
{_, [{:block_identifier, _, _} | _]} ->
|
||||
{:middle_expr, maybe_append_space(expr)}
|
||||
|
||||
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
|
||||
{:middle_expr, expr}
|
||||
|
||||
{_, [{:stab_op, _, _} | reverse_tokens]} ->
|
||||
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
|
||||
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
|
||||
|
||||
if end_index > fn_index do
|
||||
{:start_expr, expr}
|
||||
else
|
||||
{:middle_expr, expr}
|
||||
end
|
||||
|
||||
{tokens, _} ->
|
||||
case Enum.drop_while(tokens, &closing_bracket?/1) do
|
||||
[{:end, _} | _] -> {:end_expr, expr}
|
||||
_ -> {:expr, expr}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp drop_eol([{:eol, _} | rest]), do: drop_eol(rest)
|
||||
defp drop_eol(rest), do: rest
|
||||
|
||||
defp maybe_append_space([?\s]), do: [?\s]
|
||||
defp maybe_append_space([h]), do: [h, ?\s]
|
||||
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
|
||||
|
||||
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
|
||||
defp closing_bracket?(_), do: false
|
||||
|
||||
# Tokenize the buffered text by appending
|
||||
# it to the given accumulator.
|
||||
|
||||
defp tokenize_text([{_line, _column}], acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp tokenize_text(buffer, acc) do
|
||||
[{line, column} | buffer] = Enum.reverse(buffer)
|
||||
[{:text, buffer, %{line: line, column: column}} | acc]
|
||||
end
|
||||
|
||||
## Trim
|
||||
|
||||
defp trim_if_needed(rest, line, column, state, buffer) do
|
||||
if state.trim do
|
||||
buffer = trim_left(buffer, 0)
|
||||
{rest, line, column} = trim_right(rest, line, column, 0, state)
|
||||
{rest, line, column, buffer}
|
||||
else
|
||||
{rest, line, column, buffer}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_init([h | t], line, column, state) when h in @h_spaces,
|
||||
do: trim_init(t, line, column + 1, state)
|
||||
|
||||
defp trim_init([?\r, ?\n | t], line, _column, state),
|
||||
do: trim_init(t, line + 1, state.indentation + 1, state)
|
||||
|
||||
defp trim_init([?\n | t], line, _column, state),
|
||||
do: trim_init(t, line + 1, state.indentation + 1, state)
|
||||
|
||||
defp trim_init([?<, ?% | _] = rest, line, column, _state),
|
||||
do: {rest, line, column}
|
||||
|
||||
defp trim_init(_, _, _, _), do: false
|
||||
|
||||
defp trim_left(buffer, count) do
|
||||
case trim_whitespace(buffer, 0) do
|
||||
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
|
||||
{[?\n | rest], _} -> trim_left(rest, count + 1)
|
||||
_ when count > 0 -> [?\n | buffer]
|
||||
_ -> buffer
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_right(rest, line, column, last_column, state) do
|
||||
case trim_whitespace(rest, column) do
|
||||
{[?\r, ?\n | rest], column} ->
|
||||
trim_right(rest, line + 1, state.indentation + 1, column + 1, state)
|
||||
|
||||
{[?\n | rest], column} ->
|
||||
trim_right(rest, line + 1, state.indentation + 1, column, state)
|
||||
|
||||
{[], column} ->
|
||||
{[], line, column}
|
||||
|
||||
_ when last_column > 0 ->
|
||||
{[?\n | rest], line - 1, last_column}
|
||||
|
||||
_ ->
|
||||
{rest, line, column}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h | t], column) when h in @h_spaces, do: trim_whitespace(t, column + 1)
|
||||
defp trim_whitespace(list, column), do: {list, column}
|
||||
|
||||
@doc """
|
||||
This is the compilation entry point. It glues the tokenizer
|
||||
and the engine together by handling the tokens and invoking
|
||||
the engine every time a full expression or text is received.
|
||||
"""
|
||||
@spec compile([EEx.token()], String.t(), keyword) :: Macro.t()
|
||||
def compile(tokens, source, opts) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
indentation = opts[:indentation] || 0
|
||||
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
|
||||
engine = opts[:engine] || @default_engine
|
||||
|
||||
state = %{
|
||||
engine: engine,
|
||||
file: file,
|
||||
source: source,
|
||||
line: line,
|
||||
quoted: [],
|
||||
parser_options: parser_options,
|
||||
indentation: indentation
|
||||
}
|
||||
|
||||
init = state.engine.init(opts)
|
||||
generate_buffer(tokens, init, [], state)
|
||||
end
|
||||
|
||||
# Ignore tokens related to comment.
|
||||
defp generate_buffer([{:comment, _chars, _meta} | rest], buffer, scope, state) do
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
# Generates the buffers by handling each expression from the tokenizer.
|
||||
# It returns Macro.t/0 or it raises.
|
||||
|
||||
defp generate_buffer([{:text, chars, meta} | rest], buffer, scope, state) do
|
||||
buffer =
|
||||
if function_exported?(state.engine, :handle_text, 3) do
|
||||
meta = [line: meta.line, column: meta.column]
|
||||
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
|
||||
else
|
||||
# TODO: Deprecate this branch on Elixir v1.18.
|
||||
# We should most likely move this check to init to emit the deprecation once.
|
||||
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
end
|
||||
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, mark, chars, meta} | rest], buffer, scope, state) do
|
||||
options =
|
||||
[file: state.file, line: meta.line, column: column(meta.column, mark)] ++
|
||||
state.parser_options
|
||||
|
||||
expr = Code.string_to_quoted!(chars, options)
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), expr)
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:start_expr, mark, chars, meta} | rest],
|
||||
buffer,
|
||||
scope,
|
||||
state
|
||||
) do
|
||||
if mark == ~c"" do
|
||||
message =
|
||||
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
|
||||
|
||||
:elixir_errors.erl_warn({meta.line, meta.column}, state.file, message)
|
||||
def compile(source, opts) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
case EEx.Tokenizer.tokenize(source, line) do
|
||||
{:ok, tokens} ->
|
||||
state = %{engine: opts[:engine] || @default_engine,
|
||||
file: file, line: line, quoted: [], start_line: nil}
|
||||
generate_buffer(tokens, "", [], state)
|
||||
{:error, line, message} ->
|
||||
raise EEx.SyntaxError, line: line, file: file, message: message
|
||||
end
|
||||
|
||||
{rest, line, contents} = look_ahead_middle(rest, meta.line, chars) || {rest, meta.line, chars}
|
||||
start_line = meta.line
|
||||
start_column = column(meta.column, mark)
|
||||
|
||||
{contents, rest} =
|
||||
generate_buffer(
|
||||
rest,
|
||||
state.engine.handle_begin(buffer),
|
||||
[{contents, start_line, start_column} | scope],
|
||||
%{state | quoted: [], line: line}
|
||||
)
|
||||
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), contents)
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:middle_expr, ~c"", chars, meta} | rest],
|
||||
buffer,
|
||||
[{current, current_line, current_column} | scope],
|
||||
state
|
||||
) do
|
||||
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
|
||||
state = %{state | line: meta.line}
|
||||
# Generates the buffers by handling each expression from the tokenizer
|
||||
|
||||
generate_buffer(
|
||||
rest,
|
||||
state.engine.handle_begin(buffer),
|
||||
[{wrapped, current_line, current_column} | scope],
|
||||
state
|
||||
)
|
||||
defp generate_buffer([{:text, chars}|t], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, _, chars, meta} | _tokens], _buffer, [], state) do
|
||||
message = "unexpected middle of expression <%#{chars}%>"
|
||||
syntax_error!(message, meta, state)
|
||||
defp generate_buffer([{:expr, line, mark, chars}|t], buffer, scope, state) do
|
||||
expr = Code.string_to_quoted!(chars, [line: line, file: state.file])
|
||||
buffer = state.engine.handle_expr(buffer, mark, expr)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:end_expr, ~c"", chars, meta} | rest],
|
||||
buffer,
|
||||
[{current, line, column} | _],
|
||||
state
|
||||
) do
|
||||
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
|
||||
options = [file: state.file, line: line, column: column] ++ state.parser_options
|
||||
tuples = Code.string_to_quoted!(wrapped, options)
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars}|t], buffer, scope, state) do
|
||||
{contents, line, t} = look_ahead_text(t, start_line, chars)
|
||||
{contents, t} = generate_buffer(t, "", [contents|scope],
|
||||
%{state | quoted: [], line: line, start_line: start_line})
|
||||
buffer = state.engine.handle_expr(buffer, mark, contents)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, line, _, chars}|t], buffer, [current|scope], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
generate_buffer(t, "", [wrapped|scope], %{state | line: line})
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|t], buffer, [current|_], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
tuples = Code.string_to_quoted!(wrapped, [line: state.start_line, file: state.file])
|
||||
buffer = insert_quoted(tuples, state.quoted)
|
||||
{buffer, rest}
|
||||
{buffer, t}
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, _, chars, meta} | _], _buffer, [], state) do
|
||||
message = "unexpected end of expression <%#{chars}%>"
|
||||
syntax_error!(message, meta, state)
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|_], _buffer, [], state) do
|
||||
raise EEx.SyntaxError, message: "unexpected token #{inspect chars}", file: state.file, line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:eof, _meta}], buffer, [], state) do
|
||||
defp generate_buffer([], buffer, [], state) do
|
||||
state.engine.handle_body(buffer)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:eof, _meta}], _buffer, [{content, line, column} | _scope], state) do
|
||||
message = "expected a closing '<% end %>' for block expression in EEx"
|
||||
expr_meta = non_whitespace_meta(content, line, column, state)
|
||||
syntax_error!(message, expr_meta, state)
|
||||
defp generate_buffer([], _buffer, _scope, state) do
|
||||
raise EEx.SyntaxError, message: "unexpected end of string, expected a closing '<% end %>'",
|
||||
file: state.file, line: state.line
|
||||
end
|
||||
|
||||
defp non_whitespace_meta([space | rest], line, column, state) when space in @h_spaces,
|
||||
do: non_whitespace_meta(rest, line, column + 1, state)
|
||||
|
||||
defp non_whitespace_meta([?\n | rest], line, _column, state),
|
||||
do: non_whitespace_meta(rest, line + 1, state.indentation + 1, state)
|
||||
|
||||
defp non_whitespace_meta(_, line, column, _),
|
||||
do: %{line: line, column: column}
|
||||
|
||||
# Creates a placeholder and wrap it inside the expression block
|
||||
|
||||
defp wrap_expr(current, line, buffer, chars, state) do
|
||||
new_lines = List.duplicate(?\n, line - state.line)
|
||||
key = length(state.quoted)
|
||||
placeholder = ~c"__EEX__(" ++ Integer.to_charlist(key) ++ ~c");"
|
||||
count = current ++ placeholder ++ new_lines ++ chars
|
||||
new_state = %{state | quoted: [{key, state.engine.handle_end(buffer)} | state.quoted]}
|
||||
|
||||
{count, new_state}
|
||||
placeholder = '__EEX__(' ++ Integer.to_char_list(key) ++ ');'
|
||||
{current ++ placeholder ++ new_lines ++ chars,
|
||||
%{state | quoted: [{key, buffer}|state.quoted]}}
|
||||
end
|
||||
|
||||
# Look middle expressions that immediately follow a start_expr
|
||||
# Look text ahead on expressions
|
||||
|
||||
defp look_ahead_middle([{:comment, _comment, _meta} | rest], start, contents),
|
||||
do: look_ahead_middle(rest, start, contents)
|
||||
|
||||
defp look_ahead_middle([{:text, text, _meta} | rest], start, contents) do
|
||||
defp look_ahead_text([{:text, text}, {:middle_expr, line, _, chars}|t]=list, start, contents) do
|
||||
if only_spaces?(text) do
|
||||
look_ahead_middle(rest, start, contents ++ text)
|
||||
{contents ++ text ++ chars, line, t}
|
||||
else
|
||||
nil
|
||||
{contents, start, list}
|
||||
end
|
||||
end
|
||||
|
||||
defp look_ahead_middle([{:middle_expr, _, chars, meta} | rest], _start, contents) do
|
||||
{rest, meta.line, contents ++ chars}
|
||||
end
|
||||
|
||||
defp look_ahead_middle(_tokens, _start, _contents) do
|
||||
nil
|
||||
defp look_ahead_text(t, start, contents) do
|
||||
{contents, start, t}
|
||||
end
|
||||
|
||||
defp only_spaces?(chars) do
|
||||
Enum.all?(chars, &(&1 in @all_spaces))
|
||||
Enum.all?(chars, &(&1 in [?\s, ?\t, ?\r, ?\n]))
|
||||
end
|
||||
|
||||
# Changes placeholder to real expression
|
||||
|
||||
defp insert_quoted({:__EEX__, _, [key]}, quoted) do
|
||||
{^key, value} = List.keyfind(quoted, key, 0)
|
||||
{^key, value} = List.keyfind quoted, key, 0
|
||||
value
|
||||
end
|
||||
|
||||
@@ -475,49 +112,10 @@ defmodule EEx.Compiler do
|
||||
end
|
||||
|
||||
defp insert_quoted(list, quoted) when is_list(list) do
|
||||
Enum.map(list, &insert_quoted(&1, quoted))
|
||||
Enum.map list, &insert_quoted(&1, quoted)
|
||||
end
|
||||
|
||||
defp insert_quoted(other, _quoted) do
|
||||
other
|
||||
end
|
||||
|
||||
defp column(column, mark) do
|
||||
# length(~c"<%") == 2
|
||||
column + 2 + length(mark)
|
||||
end
|
||||
|
||||
defp syntax_error!(message, meta, state) do
|
||||
raise EEx.SyntaxError,
|
||||
message: message,
|
||||
snippet: code_snippet(state.source, state.indentation, meta),
|
||||
file: state.file,
|
||||
line: meta.line,
|
||||
column: meta.column
|
||||
end
|
||||
|
||||
defp code_snippet(source, indentation, meta) do
|
||||
line_start = max(meta.line - 3, 1)
|
||||
line_end = meta.line
|
||||
digits = line_end |> Integer.to_string() |> byte_size()
|
||||
number_padding = String.duplicate(" ", digits)
|
||||
indentation = String.duplicate(" ", indentation)
|
||||
|
||||
source
|
||||
|> String.split(["\r\n", "\n"])
|
||||
|> Enum.slice((line_start - 1)..(line_end - 1))
|
||||
|> Enum.map_reduce(line_start, fn
|
||||
expr, line_number when line_number == line_end ->
|
||||
arrow = String.duplicate(" ", meta.column - 1) <> "^"
|
||||
{"#{line_number} | #{indentation}#{expr}\n #{number_padding}| #{arrow}", line_number + 1}
|
||||
|
||||
expr, line_number ->
|
||||
line_number_padding = String.pad_leading("#{line_number}", digits)
|
||||
{"#{line_number_padding} | #{indentation}#{expr}", line_number + 1}
|
||||
end)
|
||||
|> case do
|
||||
{[], _} -> ""
|
||||
{snippet, _} -> Enum.join(["\n #{number_padding}|" | snippet], "\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+65
-173
@@ -2,220 +2,112 @@ defmodule EEx.Engine do
|
||||
@moduledoc ~S"""
|
||||
Basic EEx engine that ships with Elixir.
|
||||
|
||||
An engine needs to implement all callbacks below.
|
||||
An engine needs to implement three functions:
|
||||
|
||||
This module also ships with a default engine implementation
|
||||
you can delegate to. See `EEx.SmartEngine` as an example.
|
||||
* `handle_body(quoted)` - receives the final built quoted
|
||||
expression, should do final post-processing and return a
|
||||
quoted expression.
|
||||
|
||||
* `handle_text(buffer, text)` - it receives the buffer,
|
||||
the text and must return a new quoted expression.
|
||||
|
||||
* `handle_expr(buffer, marker, expr)` - it receives the buffer,
|
||||
the marker, the expr and must return a new quoted expression.
|
||||
|
||||
The marker is what follows exactly after `<%`. For example,
|
||||
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
|
||||
as marker. The allowed markers so far are: `""` and `"="`.
|
||||
|
||||
Read `handle_expr/3` below for more information about the markers
|
||||
implemented by default by this engine.
|
||||
|
||||
`EEx.Engine` can be used directly if one desires to use the
|
||||
default implementations for the functions above.
|
||||
"""
|
||||
|
||||
@type state :: term
|
||||
use Behaviour
|
||||
|
||||
@doc """
|
||||
Called at the beginning of every template.
|
||||
|
||||
It must return the initial state.
|
||||
"""
|
||||
@callback init(opts :: keyword) :: state
|
||||
|
||||
@doc """
|
||||
Called at the end of every template.
|
||||
|
||||
It must return Elixir's quoted expressions for the template.
|
||||
"""
|
||||
@callback handle_body(state) :: Macro.t()
|
||||
|
||||
@doc """
|
||||
Called for the text/static parts of a template.
|
||||
|
||||
It must return the updated state.
|
||||
"""
|
||||
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
|
||||
state
|
||||
|
||||
@doc """
|
||||
Called for the dynamic/code parts of a template.
|
||||
|
||||
The marker is what follows exactly after `<%`. For example,
|
||||
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
|
||||
as marker. The allowed markers so far are:
|
||||
|
||||
* `""`
|
||||
* `"="`
|
||||
* `"/"`
|
||||
* `"|"`
|
||||
|
||||
Markers `"/"` and `"|"` are only for use in custom EEx engines
|
||||
and are not implemented by default. Using them without an
|
||||
appropriate implementation raises `EEx.SyntaxError`.
|
||||
|
||||
It must return the updated state.
|
||||
"""
|
||||
@callback handle_expr(state, marker :: String.t(), expr :: Macro.t()) :: state
|
||||
|
||||
@doc """
|
||||
Invoked at the beginning of every nesting.
|
||||
|
||||
It must return a new state that is used only inside the nesting.
|
||||
Once the nesting terminates, the current `state` is resumed.
|
||||
"""
|
||||
@callback handle_begin(state) :: state
|
||||
|
||||
@doc """
|
||||
Invokes at the end of a nesting.
|
||||
|
||||
It must return Elixir's quoted expressions for the nesting.
|
||||
"""
|
||||
@callback handle_end(state) :: Macro.t()
|
||||
defcallback handle_body(Macro.t) :: Macro.t
|
||||
defcallback handle_text(Macro.t, binary) :: Macro.t
|
||||
defcallback handle_expr(Macro.t, binary, Macro.t) :: Macro.t
|
||||
|
||||
@doc false
|
||||
@deprecated "Use explicit delegation to EEx.Engine instead"
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def init(opts) do
|
||||
EEx.Engine.init(opts)
|
||||
def handle_body(body) do
|
||||
EEx.Engine.handle_body(body)
|
||||
end
|
||||
|
||||
def handle_body(state) do
|
||||
EEx.Engine.handle_body(state)
|
||||
def handle_text(buffer, text) do
|
||||
EEx.Engine.handle_text(buffer, text)
|
||||
end
|
||||
|
||||
def handle_begin(state) do
|
||||
EEx.Engine.handle_begin(state)
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
EEx.Engine.handle_expr(buffer, mark, expr)
|
||||
end
|
||||
|
||||
def handle_end(state) do
|
||||
EEx.Engine.handle_end(state)
|
||||
end
|
||||
|
||||
def handle_text(state, text) do
|
||||
EEx.Engine.handle_text(state, [], text)
|
||||
end
|
||||
|
||||
def handle_expr(state, marker, expr) do
|
||||
EEx.Engine.handle_expr(state, marker, expr)
|
||||
end
|
||||
|
||||
defoverridable EEx.Engine
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2]
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Handles assigns in quoted expressions.
|
||||
|
||||
A warning will be printed on missing assigns.
|
||||
Future versions will raise.
|
||||
|
||||
This can be added to any custom engine by invoking
|
||||
`handle_assign/1` with `Macro.prewalk/2`:
|
||||
`handle_assign/3` with `Macro.prewalk/1`:
|
||||
|
||||
def handle_expr(state, token, expr) do
|
||||
def handle_expr(buffer, token, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
super(state, token, expr)
|
||||
EEx.Engine.handle_expr(buffer, token, expr)
|
||||
end
|
||||
|
||||
"""
|
||||
@spec handle_assign(Macro.t()) :: Macro.t()
|
||||
def handle_assign({:@, meta, [{name, _, atom}]}) when is_atom(name) and is_atom(atom) do
|
||||
line = meta[:line] || 0
|
||||
quote(line: line, do: EEx.Engine.fetch_assign!(var!(assigns), unquote(name)))
|
||||
quote line: line, do: Dict.get(var!(assigns), unquote(name))
|
||||
end
|
||||
|
||||
def handle_assign(arg) do
|
||||
arg
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Raise on v2.0
|
||||
@spec fetch_assign!(Access.t(), Access.key()) :: term | nil
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
{:ok, val} ->
|
||||
val
|
||||
@doc """
|
||||
The default implementation implementation simply returns the
|
||||
given expression.
|
||||
"""
|
||||
def handle_body(quoted) do
|
||||
quoted
|
||||
end
|
||||
|
||||
:error ->
|
||||
keys = Enum.map(assigns, &elem(&1, 0))
|
||||
@doc """
|
||||
The default implementation simply concatenates text to the buffer.
|
||||
"""
|
||||
def handle_text(buffer, text) do
|
||||
quote do: unquote(buffer) <> unquote(text)
|
||||
end
|
||||
|
||||
IO.warn(
|
||||
"assign @#{key} not available in EEx template. " <>
|
||||
"Please ensure all assigns are given as options. " <>
|
||||
"Available assigns: #{inspect(keys)}"
|
||||
)
|
||||
@doc """
|
||||
Implements expressions according to the markers.
|
||||
|
||||
nil
|
||||
<% Elixir expression - inline with output %>
|
||||
<%= Elixir expression - replace with result %>
|
||||
|
||||
All other markers are not implemented by this engine.
|
||||
"""
|
||||
def handle_expr(buffer, "=", expr) do
|
||||
quote do
|
||||
tmp = unquote(buffer)
|
||||
tmp <> String.Chars.to_string(unquote(expr))
|
||||
end
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:init/1`."
|
||||
def init(_opts) do
|
||||
%{
|
||||
binary: [],
|
||||
dynamic: [],
|
||||
vars_count: 0
|
||||
}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_begin/1`."
|
||||
def handle_begin(state) do
|
||||
check_state!(state)
|
||||
%{state | binary: [], dynamic: []}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_end/1`."
|
||||
def handle_end(quoted) do
|
||||
handle_body(quoted)
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_body/1`."
|
||||
def handle_body(state) do
|
||||
check_state!(state)
|
||||
%{binary: binary, dynamic: dynamic} = state
|
||||
binary = {:<<>>, [], Enum.reverse(binary)}
|
||||
dynamic = [binary | dynamic]
|
||||
{:__block__, [], Enum.reverse(dynamic)}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_text/3`."
|
||||
def handle_text(state, _meta, text) do
|
||||
check_state!(state)
|
||||
%{binary: binary} = state
|
||||
%{state | binary: [text | binary]}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_expr/3`."
|
||||
def handle_expr(state, "=", ast) do
|
||||
check_state!(state)
|
||||
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
|
||||
var = Macro.var(:"arg#{vars_count}", __MODULE__)
|
||||
|
||||
ast =
|
||||
quote do
|
||||
unquote(var) = String.Chars.to_string(unquote(ast))
|
||||
end
|
||||
|
||||
segment =
|
||||
quote do
|
||||
unquote(var) :: binary
|
||||
end
|
||||
|
||||
%{state | dynamic: [ast | dynamic], binary: [segment | binary], vars_count: vars_count + 1}
|
||||
end
|
||||
|
||||
def handle_expr(state, "", ast) do
|
||||
%{dynamic: dynamic} = state
|
||||
%{state | dynamic: [ast | dynamic]}
|
||||
end
|
||||
|
||||
def handle_expr(_state, marker, _ast) when marker in ["/", "|"] do
|
||||
raise EEx.SyntaxError,
|
||||
"unsupported EEx syntax <%#{marker} %> (the syntax is valid but not supported by the current EEx engine)"
|
||||
end
|
||||
|
||||
defp check_state!(%{binary: _, dynamic: _, vars_count: _}), do: :ok
|
||||
|
||||
defp check_state!(state) do
|
||||
raise "unexpected EEx.Engine state: #{inspect(state)}. " <>
|
||||
"This typically means a bug or an outdated EEx.Engine or tool"
|
||||
def handle_expr(buffer, "", expr) do
|
||||
quote do
|
||||
tmp = unquote(buffer)
|
||||
unquote(expr)
|
||||
tmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,9 +11,9 @@ defmodule EEx.SmartEngine do
|
||||
"1"
|
||||
|
||||
In the example above, we can access the value `foo` under
|
||||
the binding `assigns` using `@foo`. This is useful because
|
||||
a template, after being compiled, can receive different
|
||||
assigns and would not require recompilation for each
|
||||
the binding `assigns` using `@foo`. This is useful when
|
||||
a template, after compiled, may receive different assigns
|
||||
and the developer don't want to recompile it for each
|
||||
variable set.
|
||||
|
||||
Assigns can also be used when compiled to a function:
|
||||
@@ -24,35 +24,18 @@ defmodule EEx.SmartEngine do
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file(:def, :sample, "sample.eex", [:assigns])
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:assigns]
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(a: 1, b: 2)
|
||||
#=> "3"
|
||||
Sample.sample(a: 1, b: 2) #=> "3"
|
||||
|
||||
"""
|
||||
|
||||
@behaviour EEx.Engine
|
||||
use EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate init(opts), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_body(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_begin(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_end(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_text(state, meta, text), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
def handle_expr(state, marker, expr) do
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
EEx.Engine.handle_expr(state, marker, expr)
|
||||
super(buffer, mark, expr)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
defmodule EEx.Tokenizer do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Tokenizes the given char list or binary.
|
||||
|
||||
It returns {:ok, list} with the following tokens:
|
||||
|
||||
* `{:text, contents}`
|
||||
* `{:expr, line, marker, contents}`
|
||||
* `{:start_expr, line, marker, contents}`
|
||||
* `{:middle_expr, line, marker, contents}`
|
||||
* `{:end_expr, line, marker, contents}`
|
||||
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
def tokenize(bin, line) when is_binary(bin) do
|
||||
tokenize(String.to_char_list(bin), line)
|
||||
end
|
||||
|
||||
def tokenize(list, line) do
|
||||
tokenize(list, line, [], [])
|
||||
end
|
||||
|
||||
defp tokenize('<%%' ++ t, line, buffer, acc) do
|
||||
case expr(t, line, [?%, ?<|buffer]) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, buffer, new_line, rest} ->
|
||||
tokenize rest, new_line, [?>, ?%|buffer], acc
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('<%#' ++ t, line, buffer, acc) do
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, _, new_line, rest} ->
|
||||
tokenize rest, new_line, buffer, acc
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('<%' ++ t, line, buffer, acc) do
|
||||
{marker, t} = retrieve_marker(t)
|
||||
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, expr, new_line, rest} ->
|
||||
token = token_name(expr)
|
||||
acc = tokenize_text(buffer, acc)
|
||||
final = {token, line, marker, Enum.reverse(expr)}
|
||||
tokenize rest, new_line, [], [final | acc]
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('\n' ++ t, line, buffer, acc) do
|
||||
tokenize t, line + 1, [?\n|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([h|t], line, buffer, acc) do
|
||||
tokenize t, line, [h|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([], _line, buffer, acc) do
|
||||
{:ok, Enum.reverse(tokenize_text(buffer, acc))}
|
||||
end
|
||||
|
||||
# Retrieve marker for <%
|
||||
|
||||
defp retrieve_marker('=' ++ t) do
|
||||
{"=", t}
|
||||
end
|
||||
|
||||
defp retrieve_marker(t) do
|
||||
{"", t}
|
||||
end
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
|
||||
defp expr([?%, ?>|t], line, buffer) do
|
||||
{:ok, buffer, line, t}
|
||||
end
|
||||
|
||||
defp expr('\n' ++ t, line, buffer) do
|
||||
expr t, line + 1, [?\n|buffer]
|
||||
end
|
||||
|
||||
defp expr([h|t], line, buffer) do
|
||||
expr t, line, [h|buffer]
|
||||
end
|
||||
|
||||
defp expr([], line, _buffer) do
|
||||
{:error, line, "missing token '%>'"}
|
||||
end
|
||||
|
||||
# Receive an expression content and check
|
||||
# if it is a start, middle or an end token.
|
||||
#
|
||||
# Start tokens finish with `do` and `fn ->`
|
||||
# Middle tokens are marked with `->` or keywords
|
||||
# End tokens contain only the end word
|
||||
|
||||
defp token_name([h|t]) when h in [?\s, ?\t] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
defp token_name('od' ++ [h|_]) when h in [?\s, ?\t, ?)] do
|
||||
:start_expr
|
||||
end
|
||||
|
||||
defp token_name('>-' ++ rest) do
|
||||
rest = Enum.reverse(rest)
|
||||
|
||||
# Tokenize the remaining passing check_terminators as
|
||||
# false, which relax the tokenizer to not error on
|
||||
# unmatched pairs. Then, we check if there is a "fn"
|
||||
# token and, if so, it is not followed by an "end"
|
||||
# token. If this is the case, we are on a start expr.
|
||||
case :elixir_tokenizer.tokenize(rest, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, _line, tokens} ->
|
||||
tokens = Enum.reverse(tokens)
|
||||
fn_index = fn_index(tokens)
|
||||
|
||||
if fn_index && end_index(tokens) > fn_index do
|
||||
:start_expr
|
||||
else
|
||||
:middle_expr
|
||||
end
|
||||
_error ->
|
||||
:middle_expr
|
||||
end
|
||||
end
|
||||
|
||||
defp token_name('esle' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('retfa' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('hctac' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('eucser' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('dne' ++ t), do: check_spaces(t, :end_expr)
|
||||
|
||||
defp token_name(_) do
|
||||
:expr
|
||||
end
|
||||
|
||||
defp fn_index(tokens) do
|
||||
Enum.find_index tokens, fn
|
||||
{:fn_paren, _} -> true
|
||||
{:fn, _} -> true
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp end_index(tokens) do
|
||||
Enum.find_index(tokens, &match?({:end, _}, &1)) || :infinity
|
||||
end
|
||||
|
||||
defp check_spaces(string, token) do
|
||||
if Enum.all?(string, &(&1 in [?\s, ?\t])) do
|
||||
token
|
||||
else
|
||||
:expr
|
||||
end
|
||||
end
|
||||
|
||||
# Tokenize the buffered text by appending
|
||||
# it to the given accumulator.
|
||||
|
||||
defp tokenize_text([], acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp tokenize_text(buffer, acc) do
|
||||
[{:text, Enum.reverse(buffer)} | acc]
|
||||
end
|
||||
end
|
||||
+4
-6
@@ -1,11 +1,9 @@
|
||||
defmodule EEx.MixProject do
|
||||
defmodule EEx.Mixfile do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :eex,
|
||||
version: System.version(),
|
||||
build_per_environment: false
|
||||
]
|
||||
[app: :eex,
|
||||
version: System.version,
|
||||
build_per_environment: false]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,59 +1,36 @@
|
||||
Code.require_file("../test_helper.exs", __DIR__)
|
||||
Code.require_file "../test_helper.exs", __DIR__
|
||||
|
||||
defmodule EEx.SmartEngineTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "evaluates simple string" do
|
||||
assert_eval("foo bar", "foo bar")
|
||||
assert_eval "foo bar", "foo bar"
|
||||
end
|
||||
|
||||
test "evaluates with assigns as keywords" do
|
||||
assert_eval("1", "<%= @foo %>", assigns: [foo: 1])
|
||||
assert_eval "1", "<%= @foo %>", assigns: [foo: 1]
|
||||
end
|
||||
|
||||
test "evaluates with assigns as a map" do
|
||||
assert_eval("1", "<%= @foo %>", assigns: %{foo: 1})
|
||||
end
|
||||
|
||||
test "error with missing assigns" do
|
||||
stderr =
|
||||
ExUnit.CaptureIO.capture_io(:stderr, fn ->
|
||||
assert_eval("", "<%= @foo %>", assigns: %{})
|
||||
end)
|
||||
|
||||
assert stderr =~ "assign @foo not available in EEx template"
|
||||
assert_eval "1", "<%= @foo %>", assigns: %{foo: 1}
|
||||
end
|
||||
|
||||
test "evaluates with loops" do
|
||||
assert_eval("1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>")
|
||||
assert_eval "1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>"
|
||||
end
|
||||
|
||||
test "preserves line numbers in assignments" do
|
||||
result = EEx.compile_string("foo\n<%= @hello %>", engine: EEx.SmartEngine)
|
||||
|
||||
test "compiled preserved line numbers" do
|
||||
result = EEx.compile_string("<%= @hello %>", engine: EEx.SmartEngine)
|
||||
Macro.prewalk(result, fn
|
||||
{_left, meta, [_, :hello]} ->
|
||||
assert Keyword.get(meta, :line) == 2
|
||||
send(self(), :found)
|
||||
|
||||
node ->
|
||||
node
|
||||
{_left, meta, _right} ->
|
||||
assert Keyword.get(meta, :line, 0) in [0, 1]
|
||||
_ ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
assert_received :found
|
||||
end
|
||||
|
||||
test "error with unused \"do\" block without \"<%=\" modifier" do
|
||||
stderr =
|
||||
ExUnit.CaptureIO.capture_io(:stderr, fn ->
|
||||
assert_eval("", "<% if true do %>I'm invisible!<% end %>", assigns: %{})
|
||||
end)
|
||||
|
||||
assert stderr =~ "the contents of this expression won't be output"
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
result = EEx.eval_string(actual, binding, file: __ENV__.file, engine: EEx.SmartEngine)
|
||||
result = EEx.eval_string(actual, binding, file: __ENV__.file)
|
||||
assert result == expected
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,408 +1,106 @@
|
||||
Code.require_file("../test_helper.exs", __DIR__)
|
||||
Code.require_file "../test_helper.exs", __DIR__
|
||||
|
||||
defmodule EEx.TokenizerTest do
|
||||
use ExUnit.Case, async: true
|
||||
require EEx.Tokenizer, as: T
|
||||
|
||||
@opts [indentation: 0, trim: false]
|
||||
|
||||
test "simple charlists" do
|
||||
assert EEx.tokenize(~c"foo", @opts) ==
|
||||
{:ok, [{:text, ~c"foo", %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
|
||||
test "simple chars lists" do
|
||||
assert T.tokenize('foo', 1) == {:ok, [{:text, 'foo'}]}
|
||||
end
|
||||
|
||||
test "simple strings" do
|
||||
assert EEx.tokenize("foo", @opts) ==
|
||||
{:ok, [{:text, ~c"foo", %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
|
||||
assert T.tokenize("foo", 1) == {:ok, [{:text, 'foo'}]}
|
||||
end
|
||||
|
||||
test "strings with embedded code" do
|
||||
assert EEx.tokenize(~c"foo <% bar %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"", ~c" bar ", %{column: 5, line: 1}},
|
||||
{:eof, %{column: 14, line: 1}}
|
||||
]}
|
||||
assert T.tokenize('foo <% bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, "", ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded equals code" do
|
||||
assert EEx.tokenize(~c"foo <%= bar %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"=", ~c" bar ", %{column: 5, line: 1}},
|
||||
{:eof, %{column: 15, line: 1}}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded slash code" do
|
||||
assert EEx.tokenize(~c"foo <%/ bar %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"/", ~c" bar ", %{column: 5, line: 1}},
|
||||
{:eof, %{column: 15, line: 1}}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded pipe code" do
|
||||
assert EEx.tokenize(~c"foo <%| bar %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"|", ~c" bar ", %{column: 5, line: 1}},
|
||||
{:eof, %{column: 15, line: 1}}
|
||||
]}
|
||||
assert T.tokenize('foo <%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, "=", ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line" do
|
||||
assert EEx.tokenize(~c"foo\n<%= bar %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo\n", %{column: 1, line: 1}},
|
||||
{:expr, ~c"=", ~c" bar ", %{column: 1, line: 2}},
|
||||
{:eof, %{column: 11, line: 2}}
|
||||
]}
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo\n'}, {:expr, 2, "=", ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line and expression with more than one line" do
|
||||
string = ~c"""
|
||||
foo <%= bar
|
||||
string = '''
|
||||
foo <%= bar
|
||||
|
||||
baz %>
|
||||
<% foo %>
|
||||
"""
|
||||
baz %>
|
||||
<% foo %>
|
||||
'''
|
||||
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"=", ~c" bar\n\nbaz ", %{column: 5, line: 1}},
|
||||
{:text, ~c"\n", %{column: 7, line: 3}},
|
||||
{:expr, ~c"", ~c" foo ", %{column: 1, line: 4}},
|
||||
{:text, ~c"\n", %{column: 10, line: 4}},
|
||||
{:eof, %{column: 1, line: 5}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(string, @opts) == {:ok, exprs}
|
||||
assert T.tokenize(string, 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:expr, 1, "=", ' bar\n\nbaz '},
|
||||
{:text, '\n'},
|
||||
{:expr, 4, "", ' foo '},
|
||||
{:text, '\n'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation" do
|
||||
assert EEx.tokenize(~c"foo <%% true %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo <% true %>", %{column: 1, line: 1}},
|
||||
{:eof, %{column: 16, line: 1}}
|
||||
]}
|
||||
assert T.tokenize('foo <%% true %>', 1) == {:ok, [
|
||||
{:text, 'foo <% true %>'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation with do-end" do
|
||||
assert EEx.tokenize(~c"foo <%% true do %>bar<%% end %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:text, ~c"foo <% true do %>bar<% end %>", %{column: 1, line: 1}},
|
||||
{:eof, %{column: 32, line: 1}}
|
||||
]}
|
||||
test "quotation with do/end" do
|
||||
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1) == {:ok, [
|
||||
{:text, 'foo <% true do %>bar<% end %>'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation with interpolation" do
|
||||
exprs = [
|
||||
{:text, ~c"a <% b ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"=", ~c" c ", %{column: 9, line: 1}},
|
||||
{:text, ~c" ", %{column: 17, line: 1}},
|
||||
{:expr, ~c"=", ~c" d ", %{column: 18, line: 1}},
|
||||
{:text, ~c" e %> f", %{column: 26, line: 1}},
|
||||
{:eof, %{column: 33, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"a <%% b <%= c %> <%= d %> e %> f", @opts) == {:ok, exprs}
|
||||
test "comments" do
|
||||
assert T.tokenize('foo <%# true %>', 1) == {:ok, [
|
||||
{:text, 'foo '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "improperly formatted quotation with interpolation" do
|
||||
exprs = [
|
||||
{:text, ~c"<%% a <%= b %> c %>", %{column: 1, line: 1}},
|
||||
{:eof, %{column: 22, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<%%% a <%%= b %> c %>", @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:eof, %{column: 16, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <%# true %>", @opts) == {:ok, exprs}
|
||||
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:eof, %{column: 8, line: 2}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <%#\ntrue %>", @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments with do-end" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:text, ~c"bar", %{column: 19, line: 1}},
|
||||
{:eof, %{column: 32, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <%# true do %>bar<%# end %>", @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments inside do-end" do
|
||||
exprs = [
|
||||
{:start_expr, ~c"", ~c" if true do ", %{column: 1, line: 1}},
|
||||
{:text, ~c"bar", %{column: 31, line: 1}},
|
||||
{:end_expr, [], ~c" end ", %{column: 34, line: 1}},
|
||||
{:eof, %{column: 43, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<% if true do %><%# comment %>bar<% end %>", @opts) == {:ok, exprs}
|
||||
|
||||
exprs = [
|
||||
{:start_expr, [], ~c" case true do ", %{column: 1, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" true -> ", %{column: 33, line: 1}},
|
||||
{:text, ~c"bar", %{column: 46, line: 1}},
|
||||
{:end_expr, [], ~c" end ", %{column: 49, line: 1}},
|
||||
{:eof, %{column: 58, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<% case true do %><%# comment %><% true -> %>bar<% end %>", @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx multi-line comments" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:comment, ~c" true ", %{column: 5, line: 1}},
|
||||
{:text, ~c" bar", %{column: 20, line: 1}},
|
||||
{:eof, %{column: 24, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <%!-- true --%> bar", @opts) == {:ok, exprs}
|
||||
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:comment, ~c" \ntrue\n ", %{column: 5, line: 1}},
|
||||
{:text, ~c" bar", %{column: 6, line: 3}},
|
||||
{:eof, %{column: 10, line: 3}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <%!-- \ntrue\n --%> bar", @opts) == {:ok, exprs}
|
||||
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:comment, ~c" <%= true %> ", %{column: 5, line: 1}},
|
||||
{:text, ~c" bar", %{column: 27, line: 1}},
|
||||
{:eof, %{column: 31, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <%!-- <%= true %> --%> bar", @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "Elixir comments" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:expr, [], ~c" true # this is a boolean ", %{column: 5, line: 1}},
|
||||
{:eof, %{column: 35, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <% true # this is a boolean %>", @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "Elixir comments with do-end" do
|
||||
exprs = [
|
||||
{:start_expr, [], ~c" if true do # startif ", %{column: 1, line: 1}},
|
||||
{:text, ~c"text", %{column: 27, line: 1}},
|
||||
{:end_expr, [], ~c" end # closeif ", %{column: 31, line: 1}},
|
||||
{:eof, %{column: 50, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<% if true do # startif %>text<% end # closeif %>", @opts) ==
|
||||
{:ok, exprs}
|
||||
test "comments with do/end" do
|
||||
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1) == {:ok, [
|
||||
{:text, 'foo bar'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded do end" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:start_expr, ~c"", ~c" if true do ", %{column: 5, line: 1}},
|
||||
{:text, ~c"bar", %{column: 21, line: 1}},
|
||||
{:end_expr, ~c"", ~c" end ", %{column: 24, line: 1}},
|
||||
{:eof, %{column: 33, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <% if true do %>bar<% end %>", @opts) == {:ok, exprs}
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, "", ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, "", ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded -> end" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:start_expr, ~c"", ~c" cond do ", %{column: 5, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" false -> ", %{column: 18, line: 1}},
|
||||
{:text, ~c"bar", %{column: 32, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" true -> ", %{column: 35, line: 1}},
|
||||
{:text, ~c"baz", %{column: 48, line: 1}},
|
||||
{:end_expr, ~c"", ~c" end ", %{column: 51, line: 1}},
|
||||
{:eof, %{column: 60, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>", @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with fn-end with newline" do
|
||||
exprs = [
|
||||
{:start_expr, ~c"=", ~c" a fn ->\n", %{column: 1, line: 1}},
|
||||
{:text, ~c"foo", %{column: 3, line: 2}},
|
||||
{:end_expr, [], ~c" end ", %{column: 6, line: 2}},
|
||||
{:eof, %{column: 15, line: 2}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<%= a fn ->\n%>foo<% end %>", @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with multiple fn-end" do
|
||||
exprs = [
|
||||
{:start_expr, ~c"=", ~c" a fn -> ", %{column: 1, line: 1}},
|
||||
{:text, ~c"foo", %{column: 15, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" end, fn -> ", %{column: 18, line: 1}},
|
||||
{:text, ~c"bar", %{column: 34, line: 1}},
|
||||
{:end_expr, ~c"", ~c" end ", %{column: 37, line: 1}},
|
||||
{:eof, %{column: 46, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<%= a fn -> %>foo<% end, fn -> %>bar<% end %>", @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with fn-end followed by do block" do
|
||||
exprs = [
|
||||
{:start_expr, ~c"=", ~c" a fn -> ", %{column: 1, line: 1}},
|
||||
{:text, ~c"foo", %{column: 15, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" end do ", %{column: 18, line: 1}},
|
||||
{:text, ~c"bar", %{column: 30, line: 1}},
|
||||
{:end_expr, ~c"", ~c" end ", %{column: 33, line: 1}},
|
||||
{:eof, %{column: 42, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"<%= a fn -> %>foo<% end do %>bar<% end %>", @opts) == {:ok, exprs}
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, "", ' cond do '},
|
||||
{:middle_expr, 1, "", ' false -> '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, "", ' true -> '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, "", ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded keywords blocks" do
|
||||
exprs = [
|
||||
{:text, ~c"foo ", %{column: 1, line: 1}},
|
||||
{:start_expr, ~c"", ~c" if true do ", %{column: 5, line: 1}},
|
||||
{:text, ~c"bar", %{column: 21, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" else ", %{column: 24, line: 1}},
|
||||
{:text, ~c"baz", %{column: 34, line: 1}},
|
||||
{:end_expr, ~c"", ~c" end ", %{column: 37, line: 1}},
|
||||
{:eof, %{column: 46, line: 1}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"foo <% if true do %>bar<% else %>baz<% end %>", @opts) ==
|
||||
{:ok, exprs}
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, "", ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, "", ' else '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, "", ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
template = ~c"\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> \n\n "
|
||||
|
||||
exprs = [
|
||||
{:start_expr, ~c"=", ~c" if true do ", %{column: 2, line: 1}},
|
||||
{:text, ~c"\n TRUE \n", %{column: 20, line: 1}},
|
||||
{:middle_expr, ~c"", ~c" else ", %{column: 3, line: 3}},
|
||||
{:text, ~c"\n FALSE \n", %{column: 13, line: 3}},
|
||||
{:end_expr, ~c"", ~c" end ", %{column: 3, line: 5}},
|
||||
{:eof, %{column: 3, line: 7}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(template, [trim: true] ++ @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode with comment" do
|
||||
exprs = [
|
||||
{:text, ~c"\n123", %{column: 19, line: 1}},
|
||||
{:eof, %{column: 4, line: 2}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c" <%# comment %> \n123", [trim: true] ++ @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode with multi-line comment" do
|
||||
exprs = [
|
||||
{:comment, ~c" comment ", %{column: 3, line: 1}},
|
||||
{:text, ~c"\n123", %{column: 23, line: 1}},
|
||||
{:eof, %{column: 4, line: 2}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c" <%!-- comment --%> \n123", [trim: true] ++ @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode with CRLF" do
|
||||
exprs = [
|
||||
{:text, ~c"0\n", %{column: 1, line: 1}},
|
||||
{:expr, ~c"=", ~c" 12 ", %{column: 3, line: 2}},
|
||||
{:text, ~c"\n34", %{column: 15, line: 2}},
|
||||
{:eof, %{column: 3, line: 3}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c"0\r\n <%= 12 %> \r\n34", [trim: true] ++ @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode set to false" do
|
||||
exprs = [
|
||||
{:text, ~c" ", %{column: 1, line: 1}},
|
||||
{:expr, ~c"=", ~c" 12 ", %{column: 2, line: 1}},
|
||||
{:text, ~c" \n", %{column: 11, line: 1}},
|
||||
{:eof, %{column: 1, line: 2}}
|
||||
]
|
||||
|
||||
assert EEx.tokenize(~c" <%= 12 %> \n", [trim: false] ++ @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode no false positives" do
|
||||
assert_not_trimmed = fn x ->
|
||||
assert EEx.tokenize(x, [trim: false] ++ @opts) == EEx.tokenize(x, @opts)
|
||||
end
|
||||
|
||||
assert_not_trimmed.(~c"foo <%= \"bar\" %> ")
|
||||
assert_not_trimmed.(~c"\n <%= \"foo\" %>bar")
|
||||
assert_not_trimmed.(~c" <%% hello %> ")
|
||||
assert_not_trimmed.(~c" <%= 01 %><%= 23 %>\n")
|
||||
end
|
||||
|
||||
test "returns error when there is start mark and no end mark" do
|
||||
message = """
|
||||
expected closing '%>' for EEx expression
|
||||
|
|
||||
1 | foo <% :bar
|
||||
| ^\
|
||||
"""
|
||||
|
||||
assert EEx.tokenize(~c"foo <% :bar", @opts) ==
|
||||
{:error, message, %{column: 5, line: 1}}
|
||||
|
||||
assert EEx.tokenize(~c"<%# true ", @opts) ==
|
||||
{:error, "expected closing '%>' for EEx expression", %{column: 1, line: 1}}
|
||||
|
||||
message = """
|
||||
expected closing '--%>' for EEx expression
|
||||
|
|
||||
1 | <%!-- foo
|
||||
| ^\
|
||||
"""
|
||||
|
||||
assert EEx.tokenize(~c"<%!-- foo", @opts) == {:error, message, %{column: 1, line: 1}}
|
||||
end
|
||||
|
||||
test "marks invalid expressions as regular expressions" do
|
||||
assert EEx.tokenize(~c"<% 1 $ 2 %>", @opts) ==
|
||||
{:ok,
|
||||
[
|
||||
{:expr, [], ~c" 1 $ 2 ", %{column: 1, line: 1}},
|
||||
{:eof, %{column: 12, line: 1}}
|
||||
]}
|
||||
test "raise syntax error when there is start mark and no end mark" do
|
||||
assert T.tokenize('foo <% :bar', 1) == {:error, 1, "missing token '%>'"}
|
||||
end
|
||||
end
|
||||
|
||||
+265
-849
File diff suppressed because it is too large
Load Diff
@@ -1 +0,0 @@
|
||||
foo <%= bar
|
||||
@@ -1,8 +1 @@
|
||||
{line_exclude, line_include} =
|
||||
if line = System.get_env("LINE"), do: {[:test], [line: line]}, else: {[], []}
|
||||
|
||||
ExUnit.start(
|
||||
trace: !!System.get_env("TRACE"),
|
||||
include: line_include,
|
||||
exclude: line_exclude
|
||||
)
|
||||
ExUnit.start [trace: "--trace" in System.argv]
|
||||
@@ -1,16 +0,0 @@
|
||||
{'src/*', [
|
||||
warn_unused_vars,
|
||||
warn_export_all,
|
||||
warn_shadow_vars,
|
||||
warn_unused_import,
|
||||
warn_unused_function,
|
||||
warn_bif_clash,
|
||||
warn_unused_record,
|
||||
warn_deprecated_function,
|
||||
warn_obsolete_guard,
|
||||
warn_exported_vars,
|
||||
%% warn_missing_spec,
|
||||
%% warn_untyped_record,
|
||||
debug_info,
|
||||
{outdir, "ebin/"}
|
||||
]}.
|
||||
+112
-1067
File diff suppressed because it is too large
Load Diff
+88
-303
@@ -6,61 +6,41 @@ defmodule Agent do
|
||||
must be accessed from different processes or by the same process
|
||||
at different points in time.
|
||||
|
||||
The `Agent` module provides a basic server implementation that
|
||||
The Agent module provides a basic server implementation that
|
||||
allows state to be retrieved and updated via a simple API.
|
||||
|
||||
## Examples
|
||||
|
||||
For example, the following agent implements a counter:
|
||||
For example, in the Mix tool that ships with Elixir, we need
|
||||
to keep a set of all tasks executed by a given project. Since
|
||||
this set is shared, we can implement it with an Agent:
|
||||
|
||||
defmodule Counter do
|
||||
use Agent
|
||||
|
||||
def start_link(initial_value) do
|
||||
Agent.start_link(fn -> initial_value end, name: __MODULE__)
|
||||
defmodule Mix.TasksServer do
|
||||
def start_link do
|
||||
Agent.start_link(fn -> HashSet.new end, name: __MODULE__)
|
||||
end
|
||||
|
||||
def value do
|
||||
Agent.get(__MODULE__, & &1)
|
||||
@doc "Checks if the task has already executed"
|
||||
def executed?(task, project) do
|
||||
item = {task, project}
|
||||
Agent.get(__MODULE__, fn set ->
|
||||
item in set
|
||||
end)
|
||||
end
|
||||
|
||||
def increment do
|
||||
Agent.update(__MODULE__, &(&1 + 1))
|
||||
@doc "Marks a task as executed"
|
||||
def put_task(task, project) do
|
||||
item = {task, project}
|
||||
Agent.update(__MODULE__, &Set.put(&1, item))
|
||||
end
|
||||
end
|
||||
|
||||
Usage would be:
|
||||
|
||||
Counter.start_link(0)
|
||||
#=> {:ok, #PID<0.123.0>}
|
||||
|
||||
Counter.value()
|
||||
#=> 0
|
||||
|
||||
Counter.increment()
|
||||
#=> :ok
|
||||
|
||||
Counter.increment()
|
||||
#=> :ok
|
||||
|
||||
Counter.value()
|
||||
#=> 2
|
||||
|
||||
Thanks to the agent server process, the counter can be safely incremented
|
||||
concurrently.
|
||||
|
||||
> #### `use Agent` {: .info}
|
||||
>
|
||||
> When you `use Agent`, the `Agent` module will define a
|
||||
> `child_spec/1` function, so your module can be used
|
||||
> as a child in a supervision tree.
|
||||
|
||||
Agents provide a segregation between the client and server APIs (similar to
|
||||
`GenServer`s). In particular, the functions passed as arguments to the calls to
|
||||
`Agent` functions are invoked inside the agent (the server). This distinction
|
||||
is important because you may want to avoid expensive operations inside the
|
||||
agent, as they will effectively block the agent until the request is
|
||||
fulfilled.
|
||||
Note that agents still provide a segregation between the
|
||||
client and server APIs, as seen in GenServers. In particular,
|
||||
all code inside the function passed to the agent is executed
|
||||
by the agent. This distinction is important because you may
|
||||
want to avoid expensive operations inside the agent, as it will
|
||||
effectively block the agent until the request is fulfilled.
|
||||
|
||||
Consider these two examples:
|
||||
|
||||
@@ -71,85 +51,25 @@ defmodule Agent do
|
||||
|
||||
# Compute in the agent/client
|
||||
def get_something(agent) do
|
||||
Agent.get(agent, & &1) |> do_something_expensive()
|
||||
Agent.get(agent, &(&1)) |> do_something_expensive()
|
||||
end
|
||||
|
||||
The first function blocks the agent. The second function copies all the state
|
||||
to the client and then executes the operation in the client. One aspect to
|
||||
consider is whether the data is large enough to require processing in the server,
|
||||
at least initially, or small enough to be sent to the client cheaply. Another
|
||||
factor is whether the data needs to be processed atomically: getting the
|
||||
state and calling `do_something_expensive(state)` outside of the agent means
|
||||
that the agent's state can be updated in the meantime. This is specially
|
||||
important in case of updates as computing the new state in the client rather
|
||||
than in the server can lead to race conditions if multiple clients are trying
|
||||
to update the same state to different values.
|
||||
The first one blocks the agent while the second one copies
|
||||
all the state to the client and executes the operation in the client.
|
||||
The trade-off here is exactly if the data is small enough to be
|
||||
sent to the client cheaply or large enough to require processing on
|
||||
the server (or at least some initial processing).
|
||||
|
||||
## How to supervise
|
||||
## Name Registration
|
||||
|
||||
An `Agent` is most commonly started under a supervision tree.
|
||||
When we invoke `use Agent`, it automatically defines a `child_spec/1`
|
||||
function that allows us to start the agent directly under a supervisor.
|
||||
To start an agent under a supervisor with an initial counter of 0,
|
||||
one may do:
|
||||
|
||||
children = [
|
||||
{Counter, 0}
|
||||
]
|
||||
|
||||
Supervisor.start_link(children, strategy: :one_for_all)
|
||||
|
||||
While one could also simply pass the `Counter` as a child to the supervisor,
|
||||
such as:
|
||||
|
||||
children = [
|
||||
Counter # Same as {Counter, []}
|
||||
]
|
||||
|
||||
Supervisor.start_link(children, strategy: :one_for_all)
|
||||
|
||||
The definition above wouldn't work for this particular example,
|
||||
as it would attempt to start the counter with an initial value
|
||||
of an empty list. However, this may be a viable option in your
|
||||
own agents. A common approach is to use a keyword list, as that
|
||||
would allow setting the initial value and giving a name to the
|
||||
counter process, for example:
|
||||
|
||||
def start_link(opts) do
|
||||
{initial_value, opts} = Keyword.pop(opts, :initial_value, 0)
|
||||
Agent.start_link(fn -> initial_value end, opts)
|
||||
end
|
||||
|
||||
and then you can use `Counter`, `{Counter, name: :my_counter}` or
|
||||
even `{Counter, initial_value: 0, name: :my_counter}` as a child
|
||||
specification.
|
||||
|
||||
`use Agent` also accepts a list of options which configures the
|
||||
child specification and therefore how it runs under a supervisor.
|
||||
The generated `child_spec/1` can be customized with the following options:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:restart` - when the child should be restarted, defaults to `:permanent`
|
||||
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
|
||||
|
||||
For example:
|
||||
|
||||
use Agent, restart: :transient, shutdown: 10_000
|
||||
|
||||
See the "Child specification" section in the `Supervisor` module for more
|
||||
detailed information. The `@doc` annotation immediately preceding
|
||||
`use Agent` will be attached to the generated `child_spec/1` function.
|
||||
|
||||
## Name registration
|
||||
|
||||
An agent is bound to the same name registration rules as GenServers.
|
||||
Read more about it in the `GenServer` documentation.
|
||||
An Agent is bound to the same name registration rules as GenServers.
|
||||
Read more about it in the `GenServer` docs.
|
||||
|
||||
## A word on distributed agents
|
||||
|
||||
It is important to consider the limitations of distributed agents. Agents
|
||||
provide two APIs, one that works with anonymous functions and another
|
||||
that expects an explicit module, function, and arguments.
|
||||
provides two APIs, one that works with anonymous functions and another
|
||||
that expects explicit module, function and arguments.
|
||||
|
||||
In a distributed setup with multiple nodes, the API that accepts anonymous
|
||||
functions only works if the caller (client) and the agent have the same
|
||||
@@ -162,21 +82,20 @@ defmodule Agent do
|
||||
In this setup, part of your environment will have one version of a given
|
||||
module and the other part another version (the newer one) of the same module.
|
||||
|
||||
The best solution is to simply use the explicit module, function, and arguments
|
||||
The best solution is to simply use the explicit module, function and arguments
|
||||
APIs when working with distributed agents.
|
||||
|
||||
## Hot code swapping
|
||||
|
||||
An agent can have its code hot swapped live by simply passing a module,
|
||||
function, and arguments tuple to the update instruction. For example, imagine
|
||||
function and args tuple to the update instruction. For example, imagine
|
||||
you have an agent named `:sample` and you want to convert its inner state
|
||||
from a keyword list to a map. It can be done with the following
|
||||
from some dict structure to a map. It can be done with the following
|
||||
instruction:
|
||||
|
||||
{:update, :sample, {:advanced, {Enum, :into, [%{}]}}}
|
||||
|
||||
The agent's state will be added to the given list of arguments (`[%{}]`) as
|
||||
the first argument.
|
||||
The agent's state will be added to the given list as the first argument.
|
||||
"""
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@@ -191,51 +110,14 @@ defmodule Agent do
|
||||
@typedoc "The agent state"
|
||||
@type state :: term
|
||||
|
||||
@doc """
|
||||
Returns a specification to start an agent under a supervisor.
|
||||
|
||||
See the "Child specification" section in the `Supervisor` module for more detailed information.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
def child_spec(arg) do
|
||||
%{
|
||||
id: Agent,
|
||||
start: {Agent, :start_link, [arg]}
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
defmacro __using__(opts) do
|
||||
quote location: :keep, bind_quoted: [opts: opts] do
|
||||
unless Module.has_attribute?(__MODULE__, :doc) do
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
See `Supervisor`.
|
||||
"""
|
||||
end
|
||||
|
||||
def child_spec(arg) do
|
||||
default = %{
|
||||
id: __MODULE__,
|
||||
start: {__MODULE__, :start_link, [arg]}
|
||||
}
|
||||
|
||||
Supervisor.child_spec(default, unquote(Macro.escape(opts)))
|
||||
end
|
||||
|
||||
defoverridable child_spec: 1
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts an agent linked to the current process with the given function.
|
||||
|
||||
This is often used to start the agent as part of a supervision tree.
|
||||
|
||||
Once the agent is spawned, the given function `fun` is invoked in the server
|
||||
process, and should return the initial agent state. Note that `start_link/2`
|
||||
does not return until the given function has returned.
|
||||
Once the agent is spawned, the given function is invoked and its return
|
||||
value is used as the agent state. Note that `start_link` does not return
|
||||
until the given function has returned.
|
||||
|
||||
## Options
|
||||
|
||||
@@ -247,7 +129,7 @@ defmodule Agent do
|
||||
and the start function will return `{:error, :timeout}`.
|
||||
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](`:sys`) will be invoked.
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
|
||||
|
||||
If the `:spawn_opt` option is present, its value will be passed as options
|
||||
to the underlying process as in `Process.spawn/4`.
|
||||
@@ -255,36 +137,26 @@ defmodule Agent do
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where `pid` is the PID of the server. If an agent with the
|
||||
specified name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the PID of that process.
|
||||
|
||||
If the given function callback fails, the function returns `{:error, reason}`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
42
|
||||
|
||||
iex> {:error, {exception, _stacktrace}} = Agent.start(fn -> raise "oops" end)
|
||||
iex> exception
|
||||
%RuntimeError{message: "oops"}
|
||||
`{:ok, pid}`, where `pid` is the pid of the server. If there already exists
|
||||
an agent with the specified name, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the given function callback fails with `reason`, the function returns
|
||||
`{:error, reason}`.
|
||||
"""
|
||||
@spec start_link((-> term), GenServer.options()) :: on_start
|
||||
@spec start_link((() -> term), GenServer.options) :: on_start
|
||||
def start_link(fun, options \\ []) when is_function(fun, 0) do
|
||||
GenServer.start_link(Agent.Server, fun, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts an agent linked to the current process.
|
||||
Starts an agent linked to the current process with the given module
|
||||
function and arguments.
|
||||
|
||||
Same as `start_link/2` but a module, function, and arguments are expected
|
||||
instead of an anonymous function; `fun` in `module` will be called with the
|
||||
given arguments `args` to initialize the state.
|
||||
Same as `start_link/2` but a module, function and args are expected
|
||||
instead of an anonymous function.
|
||||
"""
|
||||
@spec start_link(module, atom, [any], GenServer.options()) :: on_start
|
||||
@spec start_link(module, atom, [any], GenServer.options) :: on_start
|
||||
def start_link(module, fun, args, options \\ []) do
|
||||
GenServer.start_link(Agent.Server, {module, fun, args}, options)
|
||||
end
|
||||
@@ -293,48 +165,31 @@ defmodule Agent do
|
||||
Starts an agent process without links (outside of a supervision tree).
|
||||
|
||||
See `start_link/2` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start(fn -> 42 end)
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
42
|
||||
|
||||
"""
|
||||
@spec start((-> term), GenServer.options()) :: on_start
|
||||
@spec start((() -> term), GenServer.options) :: on_start
|
||||
def start(fun, options \\ []) when is_function(fun, 0) do
|
||||
GenServer.start(Agent.Server, fun, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts an agent without links with the given module, function, and arguments.
|
||||
Starts an agent with the given module function and arguments.
|
||||
|
||||
See `start_link/4` for more information.
|
||||
Similar to `start/2` but a module, function and args are expected
|
||||
instead of an anonymous function.
|
||||
"""
|
||||
@spec start(module, atom, [any], GenServer.options()) :: on_start
|
||||
@spec start(module, atom, [any], GenServer.options) :: on_start
|
||||
def start(module, fun, args, options \\ []) do
|
||||
GenServer.start(Agent.Server, {module, fun, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets an agent value via the given anonymous function.
|
||||
Gets an agent value via the given function.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The result of the function invocation is
|
||||
returned from this function.
|
||||
|
||||
`timeout` is an integer greater than zero which specifies how many
|
||||
milliseconds are allowed before the agent executes the function and returns
|
||||
the result value, or the atom `:infinity` to wait indefinitely. If no result
|
||||
is received within the specified time, the function call fails and the caller
|
||||
exits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
42
|
||||
returned.
|
||||
|
||||
A timeout can also be specified (it has a default value of 5000).
|
||||
"""
|
||||
@spec get(agent, (state -> a), timeout) :: a when a: var
|
||||
def get(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
|
||||
@@ -344,9 +199,9 @@ defmodule Agent do
|
||||
@doc """
|
||||
Gets an agent value via the given function.
|
||||
|
||||
Same as `get/3` but a module, function, and arguments are expected
|
||||
Same as `get/3` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of arguments.
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec get(agent, module, atom, [term], timeout) :: any
|
||||
def get(agent, module, fun, args, timeout \\ 5000) do
|
||||
@@ -354,28 +209,14 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets and updates the agent state in one operation via the given anonymous
|
||||
function.
|
||||
Gets and updates the agent state in one operation.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The function must return a tuple with two
|
||||
elements, the first being the value to return (that is, the "get" value)
|
||||
and the second one being the new state of the agent.
|
||||
|
||||
`timeout` is an integer greater than zero which specifies how many
|
||||
milliseconds are allowed before the agent executes the function and returns
|
||||
the result value, or the atom `:infinity` to wait indefinitely. If no result
|
||||
is received within the specified time, the function call fails and the caller
|
||||
exits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.get_and_update(pid, fn state -> {state, state + 1} end)
|
||||
42
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
43
|
||||
elements, the first being the value to return (i.e. the `get` value)
|
||||
and the second one is the new state.
|
||||
|
||||
A timeout can also be specified (it has a default value of 5000).
|
||||
"""
|
||||
@spec get_and_update(agent, (state -> {a, state}), timeout) :: a when a: var
|
||||
def get_and_update(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
|
||||
@@ -383,11 +224,11 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets and updates the agent state in one operation via the given function.
|
||||
Gets and updates the agent state in one operation.
|
||||
|
||||
Same as `get_and_update/3` but a module, function, and arguments are expected
|
||||
Same as `get_and_update/3` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of arguments.
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec get_and_update(agent, module, atom, [term], timeout) :: any
|
||||
def get_and_update(agent, module, fun, args, timeout \\ 5000) do
|
||||
@@ -395,28 +236,13 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the agent state via the given anonymous function.
|
||||
Updates the agent state.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The return value of `fun` becomes the new
|
||||
state of the agent.
|
||||
passing the agent state. The function must return the new state.
|
||||
|
||||
A timeout can also be specified (it has a default value of 5000).
|
||||
This function always returns `:ok`.
|
||||
|
||||
`timeout` is an integer greater than zero which specifies how many
|
||||
milliseconds are allowed before the agent executes the function and returns
|
||||
the result value, or the atom `:infinity` to wait indefinitely. If no result
|
||||
is received within the specified time, the function call fails and the caller
|
||||
exits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.update(pid, fn state -> state + 1 end)
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
43
|
||||
|
||||
"""
|
||||
@spec update(agent, (state -> state), timeout) :: :ok
|
||||
def update(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
|
||||
@@ -424,20 +250,11 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the agent state via the given function.
|
||||
Updates the agent state.
|
||||
|
||||
Same as `update/3` but a module, function, and arguments are expected
|
||||
Same as `update/3` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.update(pid, Kernel, :+, [12])
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
54
|
||||
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec update(agent, module, atom, [term], timeout) :: :ok
|
||||
def update(agent, module, fun, args, timeout \\ 5000) do
|
||||
@@ -445,23 +262,13 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a cast (*fire and forget*) operation on the agent state.
|
||||
Performs a cast (fire and forget) operation on the agent state.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The return value of `fun` becomes the new
|
||||
state of the agent.
|
||||
|
||||
Note that `cast` returns `:ok` immediately, regardless of whether `agent` (or
|
||||
the node it should live on) exists.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.cast(pid, fn state -> state + 1 end)
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
43
|
||||
passing the agent state. The function must return the new state.
|
||||
|
||||
Note that `cast` returns `:ok` immediately, regardless of whether the
|
||||
destination node or agent exists.
|
||||
"""
|
||||
@spec cast(agent, (state -> state)) :: :ok
|
||||
def cast(agent, fun) when is_function(fun, 1) do
|
||||
@@ -469,20 +276,11 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a cast (*fire and forget*) operation on the agent state.
|
||||
Performs a cast (fire and forget) operation on the agent state.
|
||||
|
||||
Same as `cast/2` but a module, function, and arguments are expected
|
||||
Same as `cast/2` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.cast(pid, Kernel, :+, [12])
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
54
|
||||
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec cast(agent, module, atom, [term]) :: :ok
|
||||
def cast(agent, module, fun, args) do
|
||||
@@ -490,25 +288,12 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Synchronously stops the agent with the given `reason`.
|
||||
|
||||
It returns `:ok` if the agent terminates with the given
|
||||
reason. If the agent terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.stop(pid)
|
||||
:ok
|
||||
Stops the agent.
|
||||
|
||||
Returns `:ok` if the agent is stopped within the given `timeout`.
|
||||
"""
|
||||
@spec stop(agent, reason :: term, timeout) :: :ok
|
||||
def stop(agent, reason \\ :normal, timeout \\ :infinity) do
|
||||
GenServer.stop(agent, reason, timeout)
|
||||
@spec stop(agent, timeout) :: :ok
|
||||
def stop(agent, timeout \\ 5000) do
|
||||
GenServer.call(agent, :stop, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,6 @@ defmodule Agent.Server do
|
||||
use GenServer
|
||||
|
||||
def init(fun) do
|
||||
_ = initial_call(fun)
|
||||
{:ok, run(fun, [])}
|
||||
end
|
||||
|
||||
@@ -13,39 +12,45 @@ defmodule Agent.Server do
|
||||
end
|
||||
|
||||
def handle_call({:get_and_update, fun}, _from, state) do
|
||||
case run(fun, [state]) do
|
||||
{reply, state} -> {:reply, reply, state}
|
||||
other -> {:stop, {:bad_return_value, other}, state}
|
||||
end
|
||||
{reply, state} = run(fun, [state])
|
||||
{:reply, reply, state}
|
||||
end
|
||||
|
||||
def handle_call({:update, fun}, _from, state) do
|
||||
{:reply, :ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
def handle_call(:stop, _from, state) do
|
||||
{:stop, :normal, :ok, state}
|
||||
end
|
||||
|
||||
def handle_call(msg, from, state) do
|
||||
super(msg, from, state)
|
||||
end
|
||||
|
||||
def handle_cast({:cast, fun}, state) do
|
||||
{:noreply, run(fun, [state])}
|
||||
end
|
||||
|
||||
def handle_cast(msg, state) do
|
||||
super(msg, state)
|
||||
end
|
||||
|
||||
def code_change(_old, state, fun) do
|
||||
{:ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
defp initial_call(mfa) do
|
||||
_ = Process.put(:"$initial_call", get_initial_call(mfa))
|
||||
def terminate(_reason, _state) do
|
||||
# There is a race condition if the agent is
|
||||
# restarted too fast and it is registered.
|
||||
try do
|
||||
self |> :erlang.process_info(:registered_name) |> elem(1) |> Process.unregister
|
||||
rescue
|
||||
_ -> :ok
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
defp get_initial_call(fun) when is_function(fun, 0) do
|
||||
{:module, module} = Function.info(fun, :module)
|
||||
{:name, name} = Function.info(fun, :name)
|
||||
{module, name, 0}
|
||||
end
|
||||
|
||||
defp get_initial_call({mod, fun, args}) do
|
||||
{mod, fun, length(args)}
|
||||
end
|
||||
|
||||
defp run({m, f, a}, extra), do: apply(m, f, extra ++ a)
|
||||
defp run(fun, extra), do: apply(fun, extra)
|
||||
end
|
||||
|
||||
+200
-956
File diff suppressed because it is too large
Load Diff
+8
-65
@@ -1,82 +1,25 @@
|
||||
defmodule Atom do
|
||||
@moduledoc """
|
||||
Atoms are constants whose values are their own name.
|
||||
|
||||
They are often useful to enumerate over distinct values, such as:
|
||||
|
||||
iex> :apple
|
||||
:apple
|
||||
iex> :orange
|
||||
:orange
|
||||
iex> :watermelon
|
||||
:watermelon
|
||||
|
||||
Atoms are equal if their names are equal.
|
||||
|
||||
iex> :apple == :apple
|
||||
true
|
||||
iex> :apple == :orange
|
||||
false
|
||||
|
||||
Often they are used to express the state of an operation, by using
|
||||
values such as `:ok` and `:error`.
|
||||
|
||||
The booleans `true` and `false` are also atoms:
|
||||
|
||||
iex> true == :true
|
||||
true
|
||||
iex> is_atom(false)
|
||||
true
|
||||
iex> is_boolean(:false)
|
||||
true
|
||||
|
||||
Elixir allows you to skip the leading `:` for the atoms `false`, `true`,
|
||||
and `nil`.
|
||||
|
||||
Atoms must be composed of Unicode characters such as letters, numbers,
|
||||
underscore, and `@`. If the keyword has a character that does not
|
||||
belong to the category above, such as spaces, you can wrap it in
|
||||
quotes:
|
||||
|
||||
iex> :"this is an atom with spaces"
|
||||
:"this is an atom with spaces"
|
||||
|
||||
@doc """
|
||||
Convenience functions for working with atoms.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Converts an atom to a string.
|
||||
Converts an atom to string.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_string(:foo)
|
||||
"foo"
|
||||
|
||||
"""
|
||||
@spec to_string(atom) :: String.t()
|
||||
@spec to_string(atom) :: String.t
|
||||
def to_string(atom) do
|
||||
:erlang.atom_to_binary(atom)
|
||||
:erlang.atom_to_binary(atom, :utf8)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an atom to a charlist.
|
||||
Converts an atom to a char list.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_charlist(:"An atom")
|
||||
~c"An atom"
|
||||
|
||||
"""
|
||||
@spec to_charlist(atom) :: charlist
|
||||
def to_charlist(atom) do
|
||||
@spec to_char_list(atom) :: char_list
|
||||
def to_char_list(atom) do
|
||||
:erlang.atom_to_list(atom)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Atom.to_charlist/1 instead"
|
||||
@spec to_char_list(atom) :: charlist
|
||||
def to_char_list(atom), do: Atom.to_charlist(atom)
|
||||
end
|
||||
|
||||
+324
-945
File diff suppressed because it is too large
Load Diff
+84
-55
@@ -1,36 +1,63 @@
|
||||
defmodule Behaviour do
|
||||
@moduledoc """
|
||||
Mechanism for handling behaviours.
|
||||
Utilities for defining behaviour interfaces.
|
||||
|
||||
This module is deprecated. Instead of `defcallback/1` and
|
||||
`defmacrocallback/1`, the `@callback` and `@macrocallback`
|
||||
module attributes can be used respectively. See the
|
||||
documentation for `Module` for more information on these
|
||||
attributes.
|
||||
Behaviours can be referenced by other modules
|
||||
to ensure they implement required callbacks.
|
||||
|
||||
Instead of `MyModule.__behaviour__(:callbacks)`,
|
||||
`MyModule.behaviour_info(:callbacks)` can be used.
|
||||
For example, you can specify the `URI.Parser`
|
||||
behaviour as follows:
|
||||
|
||||
defmodule URI.Parser do
|
||||
use Behaviour
|
||||
|
||||
@doc "Parses the given URL"
|
||||
defcallback parse(uri_info :: URI.t) :: URI.t
|
||||
|
||||
@doc "Defines a default port"
|
||||
defcallback default_port() :: integer
|
||||
end
|
||||
|
||||
And then a module may use it as:
|
||||
|
||||
defmodule URI.HTTP do
|
||||
@behaviour URI.Parser
|
||||
def default_port(), do: 80
|
||||
def parse(info), do: info
|
||||
end
|
||||
|
||||
If the behaviour changes or `URI.HTTP` does
|
||||
not implement one of the callbacks, a warning
|
||||
will be raised.
|
||||
|
||||
## Implementation
|
||||
|
||||
Since Erlang R15, behaviours must be defined via
|
||||
`@callback` attributes. `defcallback` is a simple
|
||||
mechanism that defines the `@callback` attribute
|
||||
according to the given type specification. `defcallback` allows
|
||||
documentation to be created for the callback and defines
|
||||
a custom function signature.
|
||||
|
||||
The callbacks and their documentation can be retrieved
|
||||
via the `__behaviour__` callback function.
|
||||
"""
|
||||
|
||||
@moduledoc deprecated: "Use @callback and @macrocallback attributes instead"
|
||||
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
Define a function callback according to the given type specification.
|
||||
"""
|
||||
@deprecated "Use the @callback module attribute instead"
|
||||
defmacro defcallback(spec) do
|
||||
do_defcallback(:def, split_spec(spec, quote(do: term)))
|
||||
do_defcallback(split_spec(spec, quote(do: term)), __CALLER__)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a macro callback according to the given type specification.
|
||||
Define a macro callback according to the given type specification.
|
||||
"""
|
||||
@deprecated "Use the @macrocallback module attribute instead"
|
||||
defmacro defmacrocallback(spec) do
|
||||
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
|
||||
do_defmacrocallback(split_spec(spec, quote(do: Macro.t)), __CALLER__)
|
||||
end
|
||||
|
||||
defp split_spec({:when, _, [{:"::", _, [spec, return]}, guard]}, _default) do
|
||||
defp split_spec({:when, _, [{:::, _, [spec, return]}, guard]}, _default) do
|
||||
{spec, return, guard}
|
||||
end
|
||||
|
||||
@@ -38,7 +65,7 @@ defmodule Behaviour do
|
||||
{spec, default, guard}
|
||||
end
|
||||
|
||||
defp split_spec({:"::", _, [spec, return]}, _default) do
|
||||
defp split_spec({:::, _, [spec, return]}, _default) do
|
||||
{spec, return, []}
|
||||
end
|
||||
|
||||
@@ -46,38 +73,40 @@ defmodule Behaviour do
|
||||
{spec, default, []}
|
||||
end
|
||||
|
||||
defp do_defcallback(kind, {spec, return, guards}) do
|
||||
defp do_defcallback({spec, return, guards}, caller) do
|
||||
case Macro.decompose_call(spec) do
|
||||
{name, args} ->
|
||||
do_callback(kind, name, args, return, guards)
|
||||
|
||||
do_callback(:def, name, args, name, length(args), args, return, guards, caller)
|
||||
_ ->
|
||||
raise ArgumentError, "invalid syntax in #{kind}callback #{Macro.to_string(spec)}"
|
||||
raise ArgumentError, "invalid syntax in defcallback #{Macro.to_string(spec)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp do_callback(kind, name, args, return, guards) do
|
||||
fun = fn
|
||||
{:"::", _, [left, right]} ->
|
||||
defp do_defmacrocallback({spec, return, guards}, caller) do
|
||||
case Macro.decompose_call(spec) do
|
||||
{name, args} ->
|
||||
do_callback(:defmacro, :"MACRO-#{name}", [quote(do: env :: Macro.Env.t)|args],
|
||||
name, length(args), args, return, guards, caller)
|
||||
_ ->
|
||||
raise ArgumentError, "invalid syntax in defmacrocallback #{Macro.to_string(spec)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp do_callback(kind, name, args, docs_name, docs_arity, _docs_args, return, guards, caller) do
|
||||
Enum.each args, fn
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
left
|
||||
|
||||
other ->
|
||||
ensure_not_default(other)
|
||||
other
|
||||
end
|
||||
|
||||
:lists.foreach(fun, args)
|
||||
|
||||
spec =
|
||||
quote do
|
||||
unquote(name)(unquote_splicing(args)) :: unquote(return) when unquote(guards)
|
||||
end
|
||||
|
||||
case kind do
|
||||
:def -> quote(do: @callback(unquote(spec)))
|
||||
:defmacro -> quote(do: @macrocallback(unquote(spec)))
|
||||
quote do
|
||||
@callback unquote(name)(unquote_splicing(args)) :: unquote(return) when unquote(guards)
|
||||
Behaviour.store_docs(__MODULE__, unquote(caller.line), unquote(kind),
|
||||
unquote(docs_name), unquote(docs_arity))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -87,37 +116,37 @@ defmodule Behaviour do
|
||||
|
||||
defp ensure_not_default(_), do: :ok
|
||||
|
||||
@doc false
|
||||
def store_docs(module, line, kind, name, arity) do
|
||||
doc = Module.get_attribute module, :doc
|
||||
Module.delete_attribute module, :doc
|
||||
Module.put_attribute module, :behaviour_docs, {{name, arity}, line, kind, doc}
|
||||
end
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
warning =
|
||||
"the Behaviour module is deprecated. Instead of using this module, " <>
|
||||
"use the @callback and @macrocallback module attributes. See the " <>
|
||||
"documentation for Module for more information on these attributes"
|
||||
Module.register_attribute(__MODULE__, :behaviour_docs, accumulate: true)
|
||||
@before_compile unquote(__MODULE__)
|
||||
import unquote(__MODULE__)
|
||||
end
|
||||
end
|
||||
|
||||
IO.warn(warning)
|
||||
@doc false
|
||||
defmacro __before_compile__(env) do
|
||||
docs = if Code.compiler_options[:docs] do
|
||||
Enum.reverse Module.get_attribute(env.module, :behaviour_docs)
|
||||
end
|
||||
|
||||
quote do
|
||||
@doc false
|
||||
def __behaviour__(:callbacks) do
|
||||
__MODULE__.behaviour_info(:callbacks)
|
||||
end
|
||||
|
||||
def __behaviour__(:docs) do
|
||||
{:docs_v1, _, :elixir, _, _, _, docs} = Code.fetch_docs(__MODULE__)
|
||||
|
||||
for {{kind, name, arity}, line, _, doc, _} <- docs, kind in [:callback, :macrocallback] do
|
||||
case kind do
|
||||
:callback -> {{name, arity}, line, :def, __behaviour__doc_value(doc)}
|
||||
:macrocallback -> {{name, arity}, line, :defmacro, __behaviour__doc_value(doc)}
|
||||
end
|
||||
end
|
||||
unquote(Macro.escape(docs))
|
||||
end
|
||||
|
||||
defp __behaviour__doc_value(%{"en" => doc}), do: doc
|
||||
defp __behaviour__doc_value(:hidden), do: false
|
||||
defp __behaviour__doc_value(_), do: nil
|
||||
|
||||
import unquote(__MODULE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+72
-214
@@ -1,40 +1,41 @@
|
||||
defmodule Bitwise do
|
||||
@moduledoc """
|
||||
A set of functions that perform calculations on bits.
|
||||
This module provides macros and operators for bitwise operators.
|
||||
These macros can be used in guards.
|
||||
|
||||
All bitwise functions work only on integers, otherwise an
|
||||
`ArithmeticError` is raised. The functions `band/2`,
|
||||
`bor/2`, `bsl/2`, and `bsr/2` also have operators,
|
||||
respectively: `&&&/2`, `|||/2`, `<<</2`, and `>>>/2`.
|
||||
The easiest way to use is to simply import them into
|
||||
your module:
|
||||
|
||||
## Guards
|
||||
iex> use Bitwise
|
||||
iex> bnot 1
|
||||
-2
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
All bitwise functions can be used in guards:
|
||||
You can select to include only or skip operators by passing options:
|
||||
|
||||
iex> odd? = fn
|
||||
...> int when Bitwise.band(int, 1) == 1 -> true
|
||||
...> _ -> false
|
||||
...> end
|
||||
iex> odd?.(1)
|
||||
true
|
||||
iex> use Bitwise, only_operators: true
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
All functions in this module are inlined by the compiler.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@deprecated "import Bitwise instead"
|
||||
@doc """
|
||||
Allow a developer to use this module in their programs with
|
||||
the following options:
|
||||
|
||||
* `:only_operators` - include only operators
|
||||
* `:skip_operators` - skip operators
|
||||
|
||||
"""
|
||||
defmacro __using__(options) do
|
||||
except =
|
||||
cond do
|
||||
Keyword.get(options, :only_operators) ->
|
||||
[bnot: 1, band: 2, bor: 2, bxor: 2, bsl: 2, bsr: 2]
|
||||
|
||||
Keyword.get(options, :skip_operators) ->
|
||||
["~~~": 1, &&&: 2, |||: 2, "^^^": 2, <<<: 2, >>>: 2]
|
||||
|
||||
true ->
|
||||
[]
|
||||
end
|
||||
except = cond do
|
||||
Keyword.get(options, :only_operators) ->
|
||||
[bnot: 1, band: 2, bor: 2, bxor: 2, bsl: 2, bsr: 2]
|
||||
Keyword.get(options, :skip_operators) ->
|
||||
[~~~: 1, &&&: 2, |||: 2, ^^^: 2, <<<: 2, >>>: 2]
|
||||
true -> []
|
||||
end
|
||||
|
||||
quote do
|
||||
import Bitwise, except: unquote(except)
|
||||
@@ -42,229 +43,86 @@ defmodule Bitwise do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise NOT of the argument.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bnot(2)
|
||||
-3
|
||||
|
||||
iex> bnot(2) &&& 3
|
||||
1
|
||||
|
||||
Bitwise not.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bnot(integer) :: integer
|
||||
def bnot(expr) do
|
||||
:erlang.bnot(expr)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def unquote(:"~~~")(expr) do
|
||||
:erlang.bnot(expr)
|
||||
defmacro bnot(expr) do
|
||||
quote do: :erlang.bnot(unquote(expr))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise AND of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> band(9, 3)
|
||||
1
|
||||
|
||||
Bitwise not as operator.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec band(integer, integer) :: integer
|
||||
def band(left, right) do
|
||||
:erlang.band(left, right)
|
||||
defmacro ~~~expr do
|
||||
quote do: :erlang.bnot(unquote(expr))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Bitwise AND operator.
|
||||
|
||||
Calculates the bitwise AND of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> 9 &&& 3
|
||||
1
|
||||
|
||||
Bitwise and.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer &&& integer :: integer
|
||||
def left &&& right do
|
||||
:erlang.band(left, right)
|
||||
defmacro band(left, right) do
|
||||
quote do: :erlang.band(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise OR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bor(9, 3)
|
||||
11
|
||||
|
||||
Bitwise and as operator.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bor(integer, integer) :: integer
|
||||
def bor(left, right) do
|
||||
:erlang.bor(left, right)
|
||||
defmacro left &&& right do
|
||||
quote do: :erlang.band(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Bitwise OR operator.
|
||||
|
||||
Calculates the bitwise OR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> 9 ||| 3
|
||||
11
|
||||
|
||||
Bitwise or.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer ||| integer :: integer
|
||||
def left ||| right do
|
||||
:erlang.bor(left, right)
|
||||
defmacro bor(left, right) do
|
||||
quote do: :erlang.bor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise XOR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bxor(9, 3)
|
||||
10
|
||||
|
||||
Bitwise or as operator.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bxor(integer, integer) :: integer
|
||||
def bxor(left, right) do
|
||||
:erlang.bxor(left, right)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def unquote(:"^^^")(left, right) do
|
||||
:erlang.bxor(left, right)
|
||||
defmacro left ||| right do
|
||||
quote do: :erlang.bor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic left bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bsl(1, 2)
|
||||
4
|
||||
|
||||
iex> bsl(1, -2)
|
||||
0
|
||||
|
||||
iex> bsl(-1, 2)
|
||||
-4
|
||||
|
||||
iex> bsl(-1, -2)
|
||||
-1
|
||||
|
||||
Bitwise xor.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bsl(integer, integer) :: integer
|
||||
def bsl(left, right) do
|
||||
:erlang.bsl(left, right)
|
||||
defmacro bxor(left, right) do
|
||||
quote do: :erlang.bxor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Arithmetic left bitshift operator.
|
||||
|
||||
Calculates the result of an arithmetic left bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> 1 <<< 2
|
||||
4
|
||||
|
||||
iex> 1 <<< -2
|
||||
0
|
||||
|
||||
iex> -1 <<< 2
|
||||
-4
|
||||
|
||||
iex> -1 <<< -2
|
||||
-1
|
||||
|
||||
Bitwise xor as operator.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer <<< integer :: integer
|
||||
def left <<< right do
|
||||
:erlang.bsl(left, right)
|
||||
defmacro left ^^^ right do
|
||||
quote do: :erlang.bxor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic right bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bsr(1, 2)
|
||||
0
|
||||
|
||||
iex> bsr(1, -2)
|
||||
4
|
||||
|
||||
iex> bsr(-1, 2)
|
||||
-1
|
||||
|
||||
iex> bsr(-1, -2)
|
||||
-4
|
||||
|
||||
Arithmetic bitshift left.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bsr(integer, integer) :: integer
|
||||
def bsr(left, right) do
|
||||
:erlang.bsr(left, right)
|
||||
defmacro bsl(left, right) do
|
||||
quote do: :erlang.bsl(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Arithmetic right bitshift operator.
|
||||
|
||||
Calculates the result of an arithmetic right bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> 1 >>> 2
|
||||
0
|
||||
|
||||
iex> 1 >>> -2
|
||||
4
|
||||
|
||||
iex> -1 >>> 2
|
||||
-1
|
||||
|
||||
iex> -1 >>> -2
|
||||
-4
|
||||
|
||||
Arithmetic bitshift left as operator.
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer >>> integer :: integer
|
||||
def left >>> right do
|
||||
:erlang.bsr(left, right)
|
||||
defmacro left <<< right do
|
||||
quote do: :erlang.bsl(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Arithmetic bitshift right.
|
||||
"""
|
||||
defmacro bsr(left, right) do
|
||||
quote do: :erlang.bsr(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Arithmetic bitshift right as operator.
|
||||
"""
|
||||
defmacro left >>> right do
|
||||
quote do: :erlang.bsr(unquote(left), unquote(right))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,973 +0,0 @@
|
||||
defmodule Calendar do
|
||||
@moduledoc """
|
||||
This module defines the responsibilities for working with
|
||||
calendars, dates, times and datetimes in Elixir.
|
||||
|
||||
It defines types and the minimal implementation
|
||||
for a calendar behaviour in Elixir. The goal of the calendar
|
||||
features in Elixir is to provide a base for interoperability
|
||||
rather than a full-featured datetime API.
|
||||
|
||||
For the actual date, time and datetime structs, see `Date`,
|
||||
`Time`, `NaiveDateTime`, and `DateTime`.
|
||||
|
||||
Types for year, month, day, and more are *overspecified*.
|
||||
For example, the `t:month/0` type is specified as an integer
|
||||
instead of `1..12`. This is because different calendars may
|
||||
have a different number of days per month.
|
||||
"""
|
||||
|
||||
@type year :: integer
|
||||
@type month :: pos_integer
|
||||
@type day :: pos_integer
|
||||
@type week :: pos_integer
|
||||
@type day_of_week :: non_neg_integer
|
||||
@type era :: non_neg_integer
|
||||
|
||||
@typedoc """
|
||||
A tuple representing the `day` and the `era`.
|
||||
"""
|
||||
@type day_of_era :: {day :: non_neg_integer(), era}
|
||||
|
||||
@type hour :: non_neg_integer
|
||||
@type minute :: non_neg_integer
|
||||
@type second :: non_neg_integer
|
||||
|
||||
@typedoc """
|
||||
The internal time format is used when converting between calendars.
|
||||
|
||||
It represents time as a fraction of a day (starting from midnight).
|
||||
`parts_in_day` specifies how much of the day is already passed,
|
||||
while `parts_per_day` signifies how many parts are there in a day.
|
||||
"""
|
||||
@type day_fraction :: {parts_in_day :: non_neg_integer, parts_per_day :: pos_integer}
|
||||
|
||||
@typedoc """
|
||||
The internal date format that is used when converting between calendars.
|
||||
|
||||
This is the number of days including the fractional part that has passed of
|
||||
the last day since `0000-01-01+00:00T00:00.000000` in ISO 8601 notation (also
|
||||
known as *midnight 1 January BC 1* of the proleptic Gregorian calendar).
|
||||
"""
|
||||
@type iso_days :: {days :: integer, day_fraction}
|
||||
|
||||
@typedoc """
|
||||
Microseconds with stored precision.
|
||||
|
||||
The precision represents the number of digits that must be used when
|
||||
representing the microseconds to external format. If the precision is `0`,
|
||||
it means microseconds must be skipped.
|
||||
"""
|
||||
@type microsecond :: {value :: non_neg_integer, precision :: non_neg_integer}
|
||||
|
||||
@typedoc "A calendar implementation."
|
||||
@type calendar :: module
|
||||
|
||||
@typedoc "The time zone ID according to the IANA tz database (for example, `Europe/Zurich`)."
|
||||
@type time_zone :: String.t()
|
||||
|
||||
@typedoc "The time zone abbreviation (for example, `CET` or `CEST` or `BST`)."
|
||||
@type zone_abbr :: String.t()
|
||||
|
||||
@typedoc """
|
||||
The time zone UTC offset in ISO seconds for standard time.
|
||||
|
||||
See also `t:std_offset/0`.
|
||||
"""
|
||||
@type utc_offset :: integer
|
||||
|
||||
@typedoc """
|
||||
The time zone standard offset in ISO seconds (typically not zero in summer times).
|
||||
|
||||
It must be added to `t:utc_offset/0` to get the total offset from UTC used for "wall time".
|
||||
"""
|
||||
@type std_offset :: integer
|
||||
|
||||
@typedoc "Any map or struct that contains the date fields."
|
||||
@type date :: %{optional(any) => any, calendar: calendar, year: year, month: month, day: day}
|
||||
|
||||
@typedoc "Any map or struct that contains the time fields."
|
||||
@type time :: %{
|
||||
optional(any) => any,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}
|
||||
|
||||
@typedoc "Any map or struct that contains the naive datetime fields."
|
||||
@type naive_datetime :: %{
|
||||
optional(any) => any,
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}
|
||||
|
||||
@typedoc "Any map or struct that contains the datetime fields."
|
||||
@type datetime :: %{
|
||||
optional(any) => any,
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: time_zone,
|
||||
zone_abbr: zone_abbr,
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset
|
||||
}
|
||||
|
||||
@typedoc """
|
||||
Specifies the time zone database for calendar operations.
|
||||
|
||||
Many functions in the `DateTime` module require a time zone database.
|
||||
By default, this module uses the default time zone database returned by
|
||||
`Calendar.get_time_zone_database/0`, which defaults to
|
||||
`Calendar.UTCOnlyTimeZoneDatabase`. This database only handles `Etc/UTC`
|
||||
datetimes and returns `{:error, :utc_only_time_zone_database}`
|
||||
for any other time zone.
|
||||
|
||||
Other time zone databases (including ones provided by packages)
|
||||
can be configured as default either via configuration:
|
||||
|
||||
config :elixir, :time_zone_database, CustomTimeZoneDatabase
|
||||
|
||||
or by calling `Calendar.put_time_zone_database/1`.
|
||||
|
||||
See `Calendar.TimeZoneDatabase` for more information on custom
|
||||
time zone databases.
|
||||
"""
|
||||
@type time_zone_database :: module()
|
||||
|
||||
@doc """
|
||||
Returns how many days there are in the given month of the given year.
|
||||
"""
|
||||
@callback days_in_month(year, month) :: day
|
||||
|
||||
@doc """
|
||||
Returns how many months there are in the given year.
|
||||
"""
|
||||
@callback months_in_year(year) :: month
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given year is a leap year.
|
||||
|
||||
A leap year is a year of a longer length than normal. The exact meaning
|
||||
is up to the calendar. A calendar must return `false` if it does not support
|
||||
the concept of leap years.
|
||||
"""
|
||||
@callback leap_year?(year) :: boolean
|
||||
|
||||
@doc """
|
||||
Calculates the day of the week from the given `year`, `month`, and `day`.
|
||||
|
||||
`starting_on` represents the starting day of the week. All
|
||||
calendars must support at least the `:default` value. They may
|
||||
also support other values representing their days of the week.
|
||||
"""
|
||||
@callback day_of_week(year, month, day, starting_on :: :default | atom) ::
|
||||
{day_of_week(), first_day_of_week :: non_neg_integer(),
|
||||
last_day_of_week :: non_neg_integer()}
|
||||
|
||||
@doc """
|
||||
Calculates the day of the year from the given `year`, `month`, and `day`.
|
||||
"""
|
||||
@callback day_of_year(year, month, day) :: non_neg_integer()
|
||||
|
||||
@doc """
|
||||
Calculates the quarter of the year from the given `year`, `month`, and `day`.
|
||||
"""
|
||||
@callback quarter_of_year(year, month, day) :: non_neg_integer()
|
||||
|
||||
@doc """
|
||||
Calculates the year and era from the given `year`.
|
||||
"""
|
||||
@callback year_of_era(year, month, day) :: {year, era}
|
||||
|
||||
@doc """
|
||||
Calculates the day and era from the given `year`, `month`, and `day`.
|
||||
"""
|
||||
@callback day_of_era(year, month, day) :: day_of_era()
|
||||
|
||||
@doc """
|
||||
Converts the date into a string according to the calendar.
|
||||
"""
|
||||
@callback date_to_string(year, month, day) :: String.t()
|
||||
|
||||
@doc """
|
||||
Converts the naive datetime (without time zone) into a string according to the calendar.
|
||||
"""
|
||||
@callback naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) ::
|
||||
String.t()
|
||||
|
||||
@doc """
|
||||
Converts the datetime (with time zone) into a string according to the calendar.
|
||||
"""
|
||||
@callback datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
time_zone,
|
||||
zone_abbr,
|
||||
utc_offset,
|
||||
std_offset
|
||||
) :: String.t()
|
||||
|
||||
@doc """
|
||||
Converts the time into a string according to the calendar.
|
||||
"""
|
||||
@callback time_to_string(hour, minute, second, microsecond) :: String.t()
|
||||
|
||||
@doc """
|
||||
Converts the datetime (without time zone) into the `t:iso_days/0` format.
|
||||
"""
|
||||
@callback naive_datetime_to_iso_days(year, month, day, hour, minute, second, microsecond) ::
|
||||
iso_days
|
||||
|
||||
@doc """
|
||||
Converts `t:iso_days/0` to the calendar's datetime format.
|
||||
"""
|
||||
@callback naive_datetime_from_iso_days(iso_days) ::
|
||||
{year, month, day, hour, minute, second, microsecond}
|
||||
|
||||
@doc """
|
||||
Converts the given time to the `t:day_fraction/0` format.
|
||||
"""
|
||||
@callback time_to_day_fraction(hour, minute, second, microsecond) :: day_fraction
|
||||
|
||||
@doc """
|
||||
Converts `t:day_fraction/0` to the calendar's time format.
|
||||
"""
|
||||
@callback time_from_day_fraction(day_fraction) :: {hour, minute, second, microsecond}
|
||||
|
||||
@doc """
|
||||
Define the rollover moment for the calendar.
|
||||
|
||||
This is the moment, in your calendar, when the current day ends
|
||||
and the next day starts.
|
||||
|
||||
The result of this function is used to check if two calendars roll over at
|
||||
the same time of day. If they do not, we can only convert datetimes and times
|
||||
between them. If they do, this means that we can also convert dates as well
|
||||
as naive datetimes between them.
|
||||
|
||||
This day fraction should be in its most simplified form possible, to make comparisons fast.
|
||||
|
||||
## Examples
|
||||
|
||||
* If in your calendar a new day starts at midnight, return `{0, 1}`.
|
||||
* If in your calendar a new day starts at sunrise, return `{1, 4}`.
|
||||
* If in your calendar a new day starts at noon, return `{1, 2}`.
|
||||
* If in your calendar a new day starts at sunset, return `{3, 4}`.
|
||||
|
||||
"""
|
||||
@callback day_rollover_relative_to_midnight_utc() :: day_fraction
|
||||
|
||||
@doc """
|
||||
Should return `true` if the given date describes a proper date in the calendar.
|
||||
"""
|
||||
@callback valid_date?(year, month, day) :: boolean
|
||||
|
||||
@doc """
|
||||
Should return `true` if the given time describes a proper time in the calendar.
|
||||
"""
|
||||
@callback valid_time?(hour, minute, second, microsecond) :: boolean
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a time returned by `c:time_to_string/4`
|
||||
into a time tuple.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_time(String.t()) ::
|
||||
{:ok, {hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a date returned by `c:date_to_string/3`
|
||||
into a date tuple.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_date(String.t()) ::
|
||||
{:ok, {year, month, day}}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a naive datetime returned by
|
||||
`c:naive_datetime_to_string/7` into a naive datetime tuple.
|
||||
|
||||
The given string may contain a timezone offset but it is ignored.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_naive_datetime(String.t()) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a datetime returned by
|
||||
`c:datetime_to_string/11` into a datetime tuple.
|
||||
|
||||
The returned datetime must be in UTC. The original `utc_offset`
|
||||
it was written in must be returned in the result.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_utc_datetime(String.t()) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Converts the given `t:iso_days/0` to the first moment of the day.
|
||||
"""
|
||||
@doc since: "1.15.0"
|
||||
@callback iso_days_to_beginning_of_day(iso_days) :: iso_days
|
||||
|
||||
@doc """
|
||||
Converts the given `t:iso_days/0` to the last moment of the day.
|
||||
"""
|
||||
@doc since: "1.15.0"
|
||||
@callback iso_days_to_end_of_day(iso_days) :: iso_days
|
||||
|
||||
@doc """
|
||||
Shifts date by given duration according to its calendar.
|
||||
"""
|
||||
@doc since: "1.17.0"
|
||||
@callback shift_date(year, month, day, Duration.t()) :: {year, month, day}
|
||||
|
||||
@doc """
|
||||
Shifts naive datetime by given duration according to its calendar.
|
||||
"""
|
||||
@doc since: "1.17.0"
|
||||
@callback shift_naive_datetime(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
Duration.t()
|
||||
) :: {year, month, day, hour, minute, second, microsecond}
|
||||
|
||||
@doc """
|
||||
Shifts time by given duration according to its calendar.
|
||||
"""
|
||||
@doc since: "1.17.0"
|
||||
@callback shift_time(hour, minute, second, microsecond, Duration.t()) ::
|
||||
{hour, minute, second, microsecond}
|
||||
|
||||
# General Helpers
|
||||
|
||||
@doc """
|
||||
Returns `true` if two calendars have the same moment of starting a new day,
|
||||
`false` otherwise.
|
||||
|
||||
If two calendars are not compatible, we can only convert datetimes and times
|
||||
between them. If they are compatible, this means that we can also convert
|
||||
dates as well as naive datetimes between them.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec compatible_calendars?(Calendar.calendar(), Calendar.calendar()) :: boolean
|
||||
def compatible_calendars?(calendar, calendar), do: true
|
||||
|
||||
def compatible_calendars?(calendar1, calendar2) do
|
||||
calendar1.day_rollover_relative_to_midnight_utc() ==
|
||||
calendar2.day_rollover_relative_to_midnight_utc()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a microsecond tuple truncated to a given precision (`:microsecond`,
|
||||
`:millisecond`, or `:second`).
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec truncate(Calendar.microsecond(), :microsecond | :millisecond | :second) ::
|
||||
Calendar.microsecond()
|
||||
def truncate(microsecond_tuple, :microsecond), do: microsecond_tuple
|
||||
|
||||
def truncate({microsecond, precision}, :millisecond) do
|
||||
output_precision = min(precision, 3)
|
||||
{div(microsecond, 1000) * 1000, output_precision}
|
||||
end
|
||||
|
||||
def truncate(_, :second), do: {0, 0}
|
||||
|
||||
@doc """
|
||||
Sets the current time zone database.
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec put_time_zone_database(time_zone_database()) :: :ok
|
||||
def put_time_zone_database(database) when is_atom(database) do
|
||||
Application.put_env(:elixir, :time_zone_database, database)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the current time zone database.
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec get_time_zone_database() :: time_zone_database()
|
||||
def get_time_zone_database() do
|
||||
Application.fetch_env!(:elixir, :time_zone_database)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the given date, time, or datetime into a string.
|
||||
|
||||
The datetime can be any of the `Calendar` types (`Time`, `Date`,
|
||||
`NaiveDateTime`, and `DateTime`) or any map, as long as they
|
||||
contain all of the relevant fields necessary for formatting.
|
||||
For example, if you use `%Y` to format the year, the datetime
|
||||
must have the `:year` field. Therefore, if you pass a `Time`,
|
||||
or a map without the `:year` field to a format that expects `%Y`,
|
||||
an error will be raised.
|
||||
|
||||
Examples of common usage:
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%y-%m-%d %I:%M:%S %p")
|
||||
"19-08-26 01:52:06 PM"
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
|
||||
"Mon, August 26 2019"
|
||||
|
||||
## User Options
|
||||
|
||||
* `:preferred_datetime` - a string for the preferred format to show datetimes,
|
||||
it can't contain the `%c` format and defaults to `"%Y-%m-%d %H:%M:%S"`
|
||||
if the option is not received
|
||||
|
||||
* `:preferred_date` - a string for the preferred format to show dates,
|
||||
it can't contain the `%x` format and defaults to `"%Y-%m-%d"`
|
||||
if the option is not received
|
||||
|
||||
* `:preferred_time` - a string for the preferred format to show times,
|
||||
it can't contain the `%X` format and defaults to `"%H:%M:%S"`
|
||||
if the option is not received
|
||||
|
||||
* `:am_pm_names` - a function that receives either `:am` or `:pm` and returns
|
||||
the name of the period of the day, if the option is not received it defaults
|
||||
to a function that returns `"am"` and `"pm"`, respectively
|
||||
|
||||
* `:month_names` - a function that receives a number and returns the name of
|
||||
the corresponding month, if the option is not received it defaults to a
|
||||
function that returns the month names in English
|
||||
|
||||
* `:abbreviated_month_names` - a function that receives a number and returns the
|
||||
abbreviated name of the corresponding month, if the option is not received it
|
||||
defaults to a function that returns the abbreviated month names in English
|
||||
|
||||
* `:day_of_week_names` - a function that receives a number and returns the name of
|
||||
the corresponding day of week, if the option is not received it defaults to a
|
||||
function that returns the day of week names in English
|
||||
|
||||
* `:abbreviated_day_of_week_names` - a function that receives a number and returns
|
||||
the abbreviated name of the corresponding day of week, if the option is not received
|
||||
it defaults to a function that returns the abbreviated day of week names in English
|
||||
|
||||
## Formatting syntax
|
||||
|
||||
The formatting syntax for the `string_format` argument is a sequence of characters in
|
||||
the following format:
|
||||
|
||||
%<padding><width><format>
|
||||
|
||||
where:
|
||||
|
||||
* `%`: indicates the start of a formatted section
|
||||
* `<padding>`: set the padding (see below)
|
||||
* `<width>`: a number indicating the minimum size of the formatted section
|
||||
* `<format>`: the format itself (see below)
|
||||
|
||||
### Accepted padding options
|
||||
|
||||
* `-`: no padding, removes all padding from the format
|
||||
* `_`: pad with spaces
|
||||
* `0`: pad with zeroes
|
||||
|
||||
### Accepted string formats
|
||||
|
||||
The accepted formats for `string_format` are:
|
||||
|
||||
Format | Description | Examples (in ISO)
|
||||
:----- | :-----------------------------------------------------------------------| :------------------------
|
||||
a | Abbreviated name of day | Mon
|
||||
A | Full name of day | Monday
|
||||
b | Abbreviated month name | Jan
|
||||
B | Full month name | January
|
||||
c | Preferred date+time representation | 2018-10-17 12:34:56
|
||||
d | Day of the month | 01, 31
|
||||
f | Microseconds *(does not support width and padding modifiers)* | 000000, 999999, 0123
|
||||
H | Hour using a 24-hour clock | 00, 23
|
||||
I | Hour using a 12-hour clock | 01, 12
|
||||
j | Day of the year | 001, 366
|
||||
m | Month | 01, 12
|
||||
M | Minute | 00, 59
|
||||
p | "AM" or "PM" (noon is "PM", midnight as "AM") | AM, PM
|
||||
P | "am" or "pm" (noon is "pm", midnight as "am") | am, pm
|
||||
q | Quarter | 1, 2, 3, 4
|
||||
s | Number of seconds since the Epoch, 1970-01-01 00:00:00+0000 (UTC) | 1565888877
|
||||
S | Second | 00, 59, 60
|
||||
u | Day of the week | 1 (Monday), 7 (Sunday)
|
||||
x | Preferred date (without time) representation | 2018-10-17
|
||||
X | Preferred time (without date) representation | 12:34:56
|
||||
y | Year as 2-digits | 01, 01, 86, 18
|
||||
Y | Year | -0001, 0001, 1986
|
||||
z | +hhmm/-hhmm time zone offset from UTC (empty string if naive) | +0300, -0530
|
||||
Z | Time zone abbreviation (empty string if naive) | CET, BRST
|
||||
% | Literal "%" character | %
|
||||
|
||||
Any other character will be interpreted as an invalid format and raise an error.
|
||||
|
||||
## Examples
|
||||
|
||||
Without user options:
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%y-%m-%d %I:%M:%S %p")
|
||||
"19-08-26 01:52:06 PM"
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
|
||||
"Mon, August 26 2019"
|
||||
|
||||
iex> Calendar.strftime(~U[2020-04-02 13:52:06.0Z], "%B %-d, %Y")
|
||||
"April 2, 2020"
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c")
|
||||
"2019-08-26 13:52:06"
|
||||
|
||||
With user options:
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c", preferred_datetime: "%H:%M:%S %d-%m-%y")
|
||||
"13:52:06 26-08-19"
|
||||
|
||||
iex> Calendar.strftime(
|
||||
...> ~U[2019-08-26 13:52:06.0Z],
|
||||
...> "%A",
|
||||
...> day_of_week_names: fn day_of_week ->
|
||||
...> {"segunda-feira", "terça-feira", "quarta-feira", "quinta-feira",
|
||||
...> "sexta-feira", "sábado", "domingo"}
|
||||
...> |> elem(day_of_week - 1)
|
||||
...> end
|
||||
...>)
|
||||
"segunda-feira"
|
||||
|
||||
iex> Calendar.strftime(
|
||||
...> ~U[2019-08-26 13:52:06.0Z],
|
||||
...> "%B",
|
||||
...> month_names: fn month ->
|
||||
...> {"січень", "лютий", "березень", "квітень", "травень", "червень",
|
||||
...> "липень", "серпень", "вересень", "жовтень", "листопад", "грудень"}
|
||||
...> |> elem(month - 1)
|
||||
...> end
|
||||
...>)
|
||||
"серпень"
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec strftime(map(), String.t(), keyword()) :: String.t()
|
||||
def strftime(date_or_time_or_datetime, string_format, user_options \\ [])
|
||||
when is_map(date_or_time_or_datetime) and is_binary(string_format) do
|
||||
parse(
|
||||
string_format,
|
||||
date_or_time_or_datetime,
|
||||
options(user_options),
|
||||
[]
|
||||
)
|
||||
|> IO.iodata_to_binary()
|
||||
end
|
||||
|
||||
defp parse("", _datetime, _format_options, acc),
|
||||
do: Enum.reverse(acc)
|
||||
|
||||
defp parse("%" <> rest, datetime, format_options, acc),
|
||||
do: parse_modifiers(rest, nil, nil, {datetime, format_options, acc})
|
||||
|
||||
defp parse(<<char, rest::binary>>, datetime, format_options, acc),
|
||||
do: parse(rest, datetime, format_options, [char | acc])
|
||||
|
||||
defp parse_modifiers("-" <> rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, "", parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers("0" <> rest, nil, nil, parser_data) do
|
||||
parse_modifiers(rest, nil, ?0, parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers("_" <> rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, ?\s, parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers(<<digit, rest::binary>>, width, pad, parser_data) when digit in ?0..?9 do
|
||||
new_width = (width || 0) * 10 + (digit - ?0)
|
||||
|
||||
parse_modifiers(rest, new_width, pad, parser_data)
|
||||
end
|
||||
|
||||
# set default padding if none was specified
|
||||
defp parse_modifiers(<<format, _::binary>> = rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, default_pad(format), parser_data)
|
||||
end
|
||||
|
||||
# set default width if none was specified
|
||||
defp parse_modifiers(<<format, _::binary>> = rest, nil, pad, parser_data) do
|
||||
parse_modifiers(rest, default_width(format), pad, parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers(rest, width, pad, {datetime, format_options, acc}) do
|
||||
format_modifiers(rest, width, pad, datetime, format_options, acc)
|
||||
end
|
||||
|
||||
defp am_pm(hour, format_options) when hour > 11 do
|
||||
format_options.am_pm_names.(:pm)
|
||||
end
|
||||
|
||||
defp am_pm(hour, format_options) when hour <= 11 do
|
||||
format_options.am_pm_names.(:am)
|
||||
end
|
||||
|
||||
defp default_pad(format) when format in ~c"aAbBpPZ", do: ?\s
|
||||
defp default_pad(_format), do: ?0
|
||||
|
||||
defp default_width(format) when format in ~c"dHImMSy", do: 2
|
||||
defp default_width(?j), do: 3
|
||||
defp default_width(format) when format in ~c"Yz", do: 4
|
||||
defp default_width(_format), do: 0
|
||||
|
||||
# Literally just %
|
||||
defp format_modifiers("%" <> rest, width, pad, datetime, format_options, acc) do
|
||||
parse(rest, datetime, format_options, [pad_leading("%", width, pad) | acc])
|
||||
end
|
||||
|
||||
# Abbreviated name of day
|
||||
defp format_modifiers("a" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime
|
||||
|> Date.day_of_week()
|
||||
|> format_options.abbreviated_day_of_week_names.()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Full name of day
|
||||
defp format_modifiers("A" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime
|
||||
|> Date.day_of_week()
|
||||
|> format_options.day_of_week_names.()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Abbreviated month name
|
||||
defp format_modifiers("b" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime.month
|
||||
|> format_options.abbreviated_month_names.()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Full month name
|
||||
defp format_modifiers("B" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.month |> format_options.month_names.() |> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Preferred date+time representation
|
||||
defp format_modifiers(
|
||||
"c" <> _rest,
|
||||
_width,
|
||||
_pad,
|
||||
_datetime,
|
||||
%{preferred_datetime_invoked: true},
|
||||
_acc
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"tried to format preferred_datetime within another preferred_datetime format"
|
||||
end
|
||||
|
||||
defp format_modifiers("c" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
format_options.preferred_datetime
|
||||
|> parse(datetime, %{format_options | preferred_datetime_invoked: true}, [])
|
||||
|> pad_preferred(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Day of the month
|
||||
defp format_modifiers("d" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.day |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Microseconds
|
||||
defp format_modifiers("f" <> rest, _width, _pad, datetime, format_options, acc) do
|
||||
{microsecond, precision} = datetime.microsecond
|
||||
|
||||
result =
|
||||
microsecond
|
||||
|> Integer.to_string()
|
||||
|> String.pad_leading(6, "0")
|
||||
|> binary_part(0, max(precision, 1))
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Hour using a 24-hour clock
|
||||
defp format_modifiers("H" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.hour |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Hour using a 12-hour clock
|
||||
defp format_modifiers("I" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = (rem(datetime.hour + 23, 12) + 1) |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Day of the year
|
||||
defp format_modifiers("j" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Date.day_of_year() |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Month
|
||||
defp format_modifiers("m" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.month |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Minute
|
||||
defp format_modifiers("M" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.minute |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# "AM" or "PM" (noon is "PM", midnight as "AM")
|
||||
defp format_modifiers("p" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.hour |> am_pm(format_options) |> String.upcase() |> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# "am" or "pm" (noon is "pm", midnight as "am")
|
||||
defp format_modifiers("P" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime.hour
|
||||
|> am_pm(format_options)
|
||||
|> String.downcase()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Quarter
|
||||
defp format_modifiers("q" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Date.quarter_of_year() |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Second
|
||||
defp format_modifiers("S" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.second |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Day of the week
|
||||
defp format_modifiers("u" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Date.day_of_week() |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Preferred date (without time) representation
|
||||
defp format_modifiers(
|
||||
"x" <> _rest,
|
||||
_width,
|
||||
_pad,
|
||||
_datetime,
|
||||
%{preferred_date_invoked: true},
|
||||
_acc
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"tried to format preferred_date within another preferred_date format"
|
||||
end
|
||||
|
||||
defp format_modifiers("x" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
format_options.preferred_date
|
||||
|> parse(datetime, %{format_options | preferred_date_invoked: true}, [])
|
||||
|> pad_preferred(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Preferred time (without date) representation
|
||||
defp format_modifiers(
|
||||
"X" <> _rest,
|
||||
_width,
|
||||
_pad,
|
||||
_datetime,
|
||||
%{preferred_time_invoked: true},
|
||||
_acc
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"tried to format preferred_time within another preferred_time format"
|
||||
end
|
||||
|
||||
defp format_modifiers("X" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
format_options.preferred_time
|
||||
|> parse(datetime, %{format_options | preferred_time_invoked: true}, [])
|
||||
|> pad_preferred(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Year as 2-digits
|
||||
defp format_modifiers("y" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.year |> rem(100) |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Year
|
||||
defp format_modifiers("Y" <> rest, width, pad, datetime, format_options, acc) do
|
||||
{sign, year} =
|
||||
if datetime.year < 0 do
|
||||
{?-, -datetime.year}
|
||||
else
|
||||
{[], datetime.year}
|
||||
end
|
||||
|
||||
result = [sign | year |> Integer.to_string() |> pad_leading(width, pad)]
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Epoch time for DateTime with time zones
|
||||
defp format_modifiers(
|
||||
"s" <> rest,
|
||||
_width,
|
||||
_pad,
|
||||
datetime = %{utc_offset: _utc_offset, std_offset: _std_offset},
|
||||
format_options,
|
||||
acc
|
||||
) do
|
||||
result =
|
||||
datetime
|
||||
|> DateTime.shift_zone!("Etc/UTC")
|
||||
|> NaiveDateTime.diff(~N[1970-01-01 00:00:00])
|
||||
|> Integer.to_string()
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Epoch time
|
||||
defp format_modifiers("s" <> rest, _width, _pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime
|
||||
|> NaiveDateTime.diff(~N[1970-01-01 00:00:00])
|
||||
|> Integer.to_string()
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# +hhmm/-hhmm time zone offset from UTC (empty string if naive)
|
||||
defp format_modifiers(
|
||||
"z" <> rest,
|
||||
width,
|
||||
pad,
|
||||
datetime = %{utc_offset: utc_offset, std_offset: std_offset},
|
||||
format_options,
|
||||
acc
|
||||
) do
|
||||
absolute_offset = abs(utc_offset + std_offset)
|
||||
|
||||
offset_number =
|
||||
Integer.to_string(div(absolute_offset, 3600) * 100 + rem(div(absolute_offset, 60), 60))
|
||||
|
||||
sign = if utc_offset + std_offset >= 0, do: "+", else: "-"
|
||||
result = "#{sign}#{pad_leading(offset_number, width, pad)}"
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
defp format_modifiers("z" <> rest, _width, _pad, datetime, format_options, acc) do
|
||||
parse(rest, datetime, format_options, ["" | acc])
|
||||
end
|
||||
|
||||
# Time zone abbreviation (empty string if naive)
|
||||
defp format_modifiers("Z" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Map.get(:zone_abbr, "") |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
defp format_modifiers(rest, _width, _pad, _datetime, _format_options, _acc) do
|
||||
{next, _rest} = String.next_grapheme(rest) || {"", ""}
|
||||
raise ArgumentError, "invalid strftime format: %#{next}"
|
||||
end
|
||||
|
||||
defp pad_preferred(result, width, pad) when length(result) < width do
|
||||
pad_preferred([pad | result], width, pad)
|
||||
end
|
||||
|
||||
defp pad_preferred(result, _width, _pad), do: result
|
||||
|
||||
defp pad_leading(string, count, padding) do
|
||||
to_pad = count - byte_size(string)
|
||||
if to_pad > 0, do: do_pad_leading(to_pad, padding, string), else: string
|
||||
end
|
||||
|
||||
defp do_pad_leading(0, _, acc), do: acc
|
||||
|
||||
defp do_pad_leading(count, padding, acc),
|
||||
do: do_pad_leading(count - 1, padding, [padding | acc])
|
||||
|
||||
defp options(user_options) do
|
||||
default_options = %{
|
||||
preferred_date: "%Y-%m-%d",
|
||||
preferred_time: "%H:%M:%S",
|
||||
preferred_datetime: "%Y-%m-%d %H:%M:%S",
|
||||
am_pm_names: fn
|
||||
:am -> "am"
|
||||
:pm -> "pm"
|
||||
end,
|
||||
month_names: fn month ->
|
||||
{"January", "February", "March", "April", "May", "June", "July", "August", "September",
|
||||
"October", "November", "December"}
|
||||
|> elem(month - 1)
|
||||
end,
|
||||
day_of_week_names: fn day_of_week ->
|
||||
{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
|> elem(day_of_week - 1)
|
||||
end,
|
||||
abbreviated_month_names: fn month ->
|
||||
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
|
||||
|> elem(month - 1)
|
||||
end,
|
||||
abbreviated_day_of_week_names: fn day_of_week ->
|
||||
{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"} |> elem(day_of_week - 1)
|
||||
end,
|
||||
preferred_datetime_invoked: false,
|
||||
preferred_date_invoked: false,
|
||||
preferred_time_invoked: false
|
||||
}
|
||||
|
||||
Enum.reduce(user_options, default_options, fn {key, value}, acc ->
|
||||
if Map.has_key?(acc, key) do
|
||||
%{acc | key => value}
|
||||
else
|
||||
raise ArgumentError, "unknown option #{inspect(key)} given to Calendar.strftime/3"
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,227 +0,0 @@
|
||||
defmodule Date.Range do
|
||||
@moduledoc """
|
||||
Returns an inclusive range between dates.
|
||||
|
||||
Ranges must be created with the `Date.range/2` or `Date.range/3` function.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
* `:first` - the initial date on the range
|
||||
* `:last` - the last date on the range
|
||||
* `:step` - (since v1.12.0) the step
|
||||
|
||||
The remaining fields are private and should not be accessed.
|
||||
"""
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
first: Date.t(),
|
||||
last: Date.t(),
|
||||
first_in_iso_days: days(),
|
||||
last_in_iso_days: days(),
|
||||
step: pos_integer | neg_integer
|
||||
}
|
||||
|
||||
@typep days() :: integer()
|
||||
|
||||
@enforce_keys [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
|
||||
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
|
||||
|
||||
defimpl Enumerable do
|
||||
def member?(
|
||||
%Date.Range{
|
||||
first: %{calendar: calendar},
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
} = range,
|
||||
%Date{calendar: calendar} = date
|
||||
) do
|
||||
{days, _} = Date.to_iso_days(date)
|
||||
|
||||
cond do
|
||||
empty?(range) ->
|
||||
{:ok, false}
|
||||
|
||||
first_days <= last_days ->
|
||||
{:ok, first_days <= days and days <= last_days and rem(days - first_days, step) == 0}
|
||||
|
||||
true ->
|
||||
{:ok, last_days <= days and days <= first_days and rem(days - first_days, step) == 0}
|
||||
end
|
||||
end
|
||||
|
||||
def member?(%Date.Range{step: _}, _) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def member?(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range,
|
||||
date
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
member?(Map.put(date_range, :step, step), date)
|
||||
end
|
||||
|
||||
def count(range) do
|
||||
{:ok, size(range)}
|
||||
end
|
||||
|
||||
def slice(
|
||||
%Date.Range{
|
||||
first_in_iso_days: first,
|
||||
first: %{calendar: calendar},
|
||||
step: step
|
||||
} = range
|
||||
) do
|
||||
{:ok, size(range), &slice(first + &1 * step, step + &3 - 1, &2, calendar)}
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def slice(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
slice(Map.put(date_range, :step, step))
|
||||
end
|
||||
|
||||
defp slice(current, _step, 1, calendar) do
|
||||
[date_from_iso_days(current, calendar)]
|
||||
end
|
||||
|
||||
defp slice(current, step, remaining, calendar) do
|
||||
[
|
||||
date_from_iso_days(current, calendar)
|
||||
| slice(current + step, step, remaining - 1, calendar)
|
||||
]
|
||||
end
|
||||
|
||||
def reduce(
|
||||
%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
first: %{calendar: calendar},
|
||||
step: step
|
||||
},
|
||||
acc,
|
||||
fun
|
||||
) do
|
||||
reduce(first_days, last_days, acc, fun, step, calendar)
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def reduce(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range,
|
||||
acc,
|
||||
fun
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
reduce(Map.put(date_range, :step, step), acc, fun)
|
||||
end
|
||||
|
||||
defp reduce(_first_days, _last_days, {:halt, acc}, _fun, _step, _calendar) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp reduce(first_days, last_days, {:suspend, acc}, fun, step, calendar) do
|
||||
{:suspended, acc, &reduce(first_days, last_days, &1, fun, step, calendar)}
|
||||
end
|
||||
|
||||
defp reduce(first_days, last_days, {:cont, acc}, fun, step, calendar)
|
||||
when step > 0 and first_days <= last_days
|
||||
when step < 0 and first_days >= last_days do
|
||||
reduce(
|
||||
first_days + step,
|
||||
last_days,
|
||||
fun.(date_from_iso_days(first_days, calendar), acc),
|
||||
fun,
|
||||
step,
|
||||
calendar
|
||||
)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _step, _calendar) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
defp date_from_iso_days(days, Calendar.ISO) do
|
||||
{year, month, day} = Calendar.ISO.date_from_iso_days(days)
|
||||
%Date{year: year, month: month, day: day, calendar: Calendar.ISO}
|
||||
end
|
||||
|
||||
defp date_from_iso_days(days, calendar) do
|
||||
{year, month, day, _, _, _, _} =
|
||||
calendar.naive_datetime_from_iso_days({days, {0, 86_400_000_000}})
|
||||
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
|
||||
when step > 0 and first_days > last_days,
|
||||
do: 0
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
|
||||
when step < 0 and first_days < last_days,
|
||||
do: 0
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
|
||||
do: abs(div(last_days - first_days, step)) + 1
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
defp size(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
size(Map.put(date_range, :step, step))
|
||||
end
|
||||
|
||||
defp empty?(%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
})
|
||||
when step > 0 and first_days > last_days,
|
||||
do: true
|
||||
|
||||
defp empty?(%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
})
|
||||
when step < 0 and first_days < last_days,
|
||||
do: true
|
||||
|
||||
defp empty?(%Date.Range{step: _}), do: false
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
defp empty?(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
empty?(Map.put(date_range, :step, step))
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
import Kernel, except: [inspect: 2]
|
||||
|
||||
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
|
||||
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ")"
|
||||
end
|
||||
|
||||
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
|
||||
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ", #{step})"
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def inspect(%{__struct__: Date.Range, first: first, last: last} = date_range, opts) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
inspect(Map.put(date_range, :step, step), opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,426 +0,0 @@
|
||||
defmodule Duration do
|
||||
@moduledoc """
|
||||
Struct and functions for handling durations.
|
||||
|
||||
A `Duration` struct represents a collection of time scale units,
|
||||
allowing for manipulation and calculation of durations.
|
||||
|
||||
Date and time scale units are represented as integers, allowing for
|
||||
both positive and negative values.
|
||||
|
||||
Microseconds are represented using a tuple `{microsecond, precision}`.
|
||||
This ensures compatibility with other calendar types implementing time,
|
||||
such as `Time`, `DateTime`, and `NaiveDateTime`.
|
||||
|
||||
## Shifting
|
||||
|
||||
The most common use of durations in Elixir's standard library is to
|
||||
"shift" the calendar types.
|
||||
|
||||
iex> Date.shift(~D[2016-01-03], month: 2)
|
||||
~D[2016-03-03]
|
||||
|
||||
In the example above, `Date.shift/2` automatically converts the units
|
||||
into a `Duration` struct, although one can also be given directly:
|
||||
|
||||
iex> Date.shift(~D[2016-01-03], Duration.new!(month: 2))
|
||||
~D[2016-03-03]
|
||||
|
||||
It is important to notice that shifting is not an arithmetic operation.
|
||||
For example, adding `date + 1 month + 1 month` does not yield the same
|
||||
result as `date + 2 months`. Let's see an example:
|
||||
|
||||
iex> ~D[2016-01-31] |> Date.shift(month: 1) |> Date.shift(month: 1)
|
||||
~D[2016-03-29]
|
||||
|
||||
iex> ~D[2016-01-31] |> Date.shift(month: 2)
|
||||
~D[2016-03-31]
|
||||
|
||||
As you can see above, the results differ, which explains why operations
|
||||
with durations are called "shift" rather than "add". This happens because,
|
||||
once we add one month to `2016-01-31`, we get `2016-02-29`. Then adding
|
||||
one extra month gives us `2016-03-29` instead of `2016-03-31`.
|
||||
|
||||
In particular, when applying durations to `Calendar.ISO` types:
|
||||
|
||||
* larger units (such as years and months) are applied before
|
||||
smaller ones (such as weeks, hours, days, and so on)
|
||||
|
||||
* in case of non-existing dates, the results are rounded down to the
|
||||
nearest valid date
|
||||
|
||||
## Intervals
|
||||
|
||||
Durations in Elixir can be combined with stream operations to build intervals.
|
||||
For example, to retrieve the next three Wednesdays starting from 17th April, 2024:
|
||||
|
||||
iex> ~D[2024-04-17] |> Stream.iterate(&Date.shift(&1, week: 1)) |> Enum.take(3)
|
||||
[~D[2024-04-17], ~D[2024-04-24], ~D[2024-05-01]]
|
||||
|
||||
However, once again, it is important to remember that shifting a duration is not
|
||||
arithmetic, so you may want to use the functions in this module depending on what
|
||||
you to achieve. Compare the results of both examples below:
|
||||
|
||||
# Adding one month after the other
|
||||
iex> date = ~D[2016-01-31]
|
||||
iex> duration = Duration.new!(month: 1)
|
||||
iex> stream = Stream.iterate(date, fn prev_date -> Date.shift(prev_date, duration) end)
|
||||
iex> Enum.take(stream, 3)
|
||||
[~D[2016-01-31], ~D[2016-02-29], ~D[2016-03-29]]
|
||||
|
||||
# Multiplying durations by an index
|
||||
iex> date = ~D[2016-01-31]
|
||||
iex> duration = Duration.new!(month: 1)
|
||||
iex> stream = Stream.from_index(fn i -> Date.shift(date, Duration.multiply(duration, i)) end)
|
||||
iex> Enum.take(stream, 3)
|
||||
[~D[2016-01-31], ~D[2016-02-29], ~D[2016-03-31]]
|
||||
|
||||
The second example consistently points to the last day of the month,
|
||||
as it performs operations on the duration, rather than shifting date
|
||||
after date.
|
||||
"""
|
||||
|
||||
@moduledoc since: "1.17.0"
|
||||
|
||||
@derive {Inspect, optional: [:year, :month, :week, :day, :hour, :minute, :second, :microsecond]}
|
||||
defstruct year: 0,
|
||||
month: 0,
|
||||
week: 0,
|
||||
day: 0,
|
||||
hour: 0,
|
||||
minute: 0,
|
||||
second: 0,
|
||||
microsecond: {0, 0}
|
||||
|
||||
@typedoc """
|
||||
The duration struct type.
|
||||
"""
|
||||
@type t :: %Duration{
|
||||
year: integer,
|
||||
month: integer,
|
||||
week: integer,
|
||||
day: integer,
|
||||
hour: integer,
|
||||
minute: integer,
|
||||
second: integer,
|
||||
microsecond: {integer, 0..6}
|
||||
}
|
||||
|
||||
@typedoc """
|
||||
The unit pair type specifies a pair of a valid duration unit key and value.
|
||||
"""
|
||||
@type unit_pair ::
|
||||
{:year, integer}
|
||||
| {:month, integer}
|
||||
| {:week, integer}
|
||||
| {:day, integer}
|
||||
| {:hour, integer}
|
||||
| {:minute, integer}
|
||||
| {:second, integer}
|
||||
| {:microsecond, {integer, 0..6}}
|
||||
|
||||
@typedoc """
|
||||
The duration type specifies a `%Duration{}` struct or a keyword list of valid duration unit pairs.
|
||||
"""
|
||||
@type duration :: t | [unit_pair]
|
||||
|
||||
@microseconds_per_second 1_000_000
|
||||
|
||||
@doc """
|
||||
Creates a new `Duration` struct from given `unit_pairs`.
|
||||
|
||||
Raises an `ArgumentError` when called with invalid unit pairs.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.new!(year: 1, week: 3, hour: 4, second: 1)
|
||||
%Duration{year: 1, week: 3, hour: 4, second: 1}
|
||||
iex> Duration.new!(second: 1, microsecond: {1000, 6})
|
||||
%Duration{second: 1, microsecond: {1000, 6}}
|
||||
iex> Duration.new!(month: 2)
|
||||
%Duration{month: 2}
|
||||
|
||||
"""
|
||||
@spec new!(duration()) :: t
|
||||
def new!(%Duration{} = duration) do
|
||||
duration
|
||||
end
|
||||
|
||||
def new!(unit_pairs) do
|
||||
Enum.each(unit_pairs, &validate_unit!/1)
|
||||
struct!(Duration, unit_pairs)
|
||||
end
|
||||
|
||||
defp validate_unit!({:microsecond, {ms, precision}})
|
||||
when is_integer(ms) and precision in 0..6 do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp validate_unit!({:microsecond, microsecond}) do
|
||||
raise ArgumentError,
|
||||
"unsupported value #{inspect(microsecond)} for :microsecond. Expected a tuple {ms, precision} where precision is an integer from 0 to 6"
|
||||
end
|
||||
|
||||
defp validate_unit!({unit, _value})
|
||||
when unit not in [:year, :month, :week, :day, :hour, :minute, :second] do
|
||||
raise ArgumentError,
|
||||
"unknown unit #{inspect(unit)}. Expected :year, :month, :week, :day, :hour, :minute, :second, :microsecond"
|
||||
end
|
||||
|
||||
defp validate_unit!({_unit, value}) when is_integer(value) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp validate_unit!({unit, value}) do
|
||||
raise ArgumentError,
|
||||
"unsupported value #{inspect(value)} for #{inspect(unit)}. Expected an integer"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Adds units of given durations `d1` and `d2`.
|
||||
|
||||
Respects the the highest microsecond precision of the two.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.add(Duration.new!(week: 2, day: 1), Duration.new!(day: 2))
|
||||
%Duration{week: 2, day: 3}
|
||||
iex> Duration.add(Duration.new!(microsecond: {400, 3}), Duration.new!(microsecond: {600, 6}))
|
||||
%Duration{microsecond: {1000, 6}}
|
||||
|
||||
"""
|
||||
@spec add(t, t) :: t
|
||||
def add(%Duration{} = d1, %Duration{} = d2) do
|
||||
{m1, p1} = d1.microsecond
|
||||
{m2, p2} = d2.microsecond
|
||||
|
||||
%Duration{
|
||||
year: d1.year + d2.year,
|
||||
month: d1.month + d2.month,
|
||||
week: d1.week + d2.week,
|
||||
day: d1.day + d2.day,
|
||||
hour: d1.hour + d2.hour,
|
||||
minute: d1.minute + d2.minute,
|
||||
second: d1.second + d2.second,
|
||||
microsecond: {m1 + m2, max(p1, p2)}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Subtracts units of given durations `d1` and `d2`.
|
||||
|
||||
Respects the the highest microsecond precision of the two.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.subtract(Duration.new!(week: 2, day: 1), Duration.new!(day: 2))
|
||||
%Duration{week: 2, day: -1}
|
||||
iex> Duration.subtract(Duration.new!(microsecond: {400, 6}), Duration.new!(microsecond: {600, 3}))
|
||||
%Duration{microsecond: {-200, 6}}
|
||||
|
||||
"""
|
||||
@spec subtract(t, t) :: t
|
||||
def subtract(%Duration{} = d1, %Duration{} = d2) do
|
||||
{m1, p1} = d1.microsecond
|
||||
{m2, p2} = d2.microsecond
|
||||
|
||||
%Duration{
|
||||
year: d1.year - d2.year,
|
||||
month: d1.month - d2.month,
|
||||
week: d1.week - d2.week,
|
||||
day: d1.day - d2.day,
|
||||
hour: d1.hour - d2.hour,
|
||||
minute: d1.minute - d2.minute,
|
||||
second: d1.second - d2.second,
|
||||
microsecond: {m1 - m2, max(p1, p2)}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Multiplies `duration` units by given `integer`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.multiply(Duration.new!(day: 1, minute: 15, second: -10), 3)
|
||||
%Duration{day: 3, minute: 45, second: -30}
|
||||
iex> Duration.multiply(Duration.new!(microsecond: {200, 4}), 3)
|
||||
%Duration{microsecond: {600, 4}}
|
||||
|
||||
"""
|
||||
@spec multiply(t, integer) :: t
|
||||
def multiply(%Duration{microsecond: {ms, p}} = duration, integer) when is_integer(integer) do
|
||||
%Duration{
|
||||
year: duration.year * integer,
|
||||
month: duration.month * integer,
|
||||
week: duration.week * integer,
|
||||
day: duration.day * integer,
|
||||
hour: duration.hour * integer,
|
||||
minute: duration.minute * integer,
|
||||
second: duration.second * integer,
|
||||
microsecond: {ms * integer, p}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Negates `duration` units.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.negate(Duration.new!(day: 1, minute: 15, second: -10))
|
||||
%Duration{day: -1, minute: -15, second: 10}
|
||||
iex> Duration.negate(Duration.new!(microsecond: {500000, 4}))
|
||||
%Duration{microsecond: {-500000, 4}}
|
||||
|
||||
"""
|
||||
@spec negate(t) :: t
|
||||
def negate(%Duration{microsecond: {ms, p}} = duration) do
|
||||
%Duration{
|
||||
year: -duration.year,
|
||||
month: -duration.month,
|
||||
week: -duration.week,
|
||||
day: -duration.day,
|
||||
hour: -duration.hour,
|
||||
minute: -duration.minute,
|
||||
second: -duration.second,
|
||||
microsecond: {-ms, p}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) formatted duration string to a `Duration` struct.
|
||||
|
||||
Duration strings, as well as individual units, may be prefixed with plus/minus signs so that:
|
||||
|
||||
- `-PT6H3M` parses as `%Duration{hour: -6, minute: -3}`
|
||||
- `-PT6H-3M` parses as `%Duration{hour: -6, minute: 3}`
|
||||
- `+PT6H3M` parses as `%Duration{hour: 6, minute: 3}`
|
||||
- `+PT6H-3M` parses as `%Duration{hour: 6, minute: -3}`
|
||||
|
||||
Duration designators must be provided in order of magnitude: `P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S`.
|
||||
|
||||
Only seconds may be specified with a decimal fraction, using either a comma or a full stop: `P1DT4,5S`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.from_iso8601("P1Y2M3DT4H5M6S")
|
||||
{:ok, %Duration{year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6}}
|
||||
iex> Duration.from_iso8601("P3Y-2MT3H")
|
||||
{:ok, %Duration{year: 3, month: -2, hour: 3}}
|
||||
iex> Duration.from_iso8601("-PT10H-30M")
|
||||
{:ok, %Duration{hour: -10, minute: 30}}
|
||||
iex> Duration.from_iso8601("PT4.650S")
|
||||
{:ok, %Duration{second: 4, microsecond: {650000, 3}}}
|
||||
|
||||
"""
|
||||
@spec from_iso8601(String.t()) :: {:ok, t} | {:error, atom}
|
||||
def from_iso8601(string) when is_binary(string) do
|
||||
case Calendar.ISO.parse_duration(string) do
|
||||
{:ok, duration} ->
|
||||
{:ok, new!(duration)}
|
||||
|
||||
error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `from_iso8601/1` but raises an `ArgumentError`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.from_iso8601!("P1Y2M3DT4H5M6S")
|
||||
%Duration{year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6}
|
||||
iex> Duration.from_iso8601!("P10D")
|
||||
%Duration{day: 10}
|
||||
|
||||
"""
|
||||
@spec from_iso8601!(String.t()) :: t
|
||||
def from_iso8601!(string) when is_binary(string) do
|
||||
case from_iso8601(string) do
|
||||
{:ok, duration} ->
|
||||
duration
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError, ~s/failed to parse duration "#{string}". reason: #{inspect(reason)}/
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `duration` to an [ISO 8601-2:2019](https://en.wikipedia.org/wiki/ISO_8601) formatted string.
|
||||
|
||||
Note this function implements the *extension* of ISO 8601:2019. This extensions allows weeks to
|
||||
appear between months and days: `P3M3W3D`, making it fully compatible with any `Duration` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Duration.to_iso8601(Duration.new!(year: 3))
|
||||
"P3Y"
|
||||
iex> Duration.to_iso8601(Duration.new!(day: 40, hour: 12, minute: 42, second: 12))
|
||||
"P40DT12H42M12S"
|
||||
iex> Duration.to_iso8601(Duration.new!(second: 30))
|
||||
"PT30S"
|
||||
|
||||
iex> Duration.to_iso8601(Duration.new!([]))
|
||||
"PT0S"
|
||||
|
||||
iex> Duration.to_iso8601(Duration.new!(second: 1, microsecond: {2_200, 3}))
|
||||
"PT1.002S"
|
||||
iex> Duration.to_iso8601(Duration.new!(second: 1, microsecond: {-1_200_000, 4}))
|
||||
"PT-0.2000S"
|
||||
"""
|
||||
|
||||
@spec to_iso8601(t) :: String.t()
|
||||
def to_iso8601(%Duration{} = duration) do
|
||||
case {to_iso8601_duration_date(duration), to_iso8601_duration_time(duration)} do
|
||||
{[], []} -> "PT0S"
|
||||
{date, time} -> IO.iodata_to_binary([?P, date, time])
|
||||
end
|
||||
end
|
||||
|
||||
defp to_iso8601_duration_date(%{year: 0, month: 0, week: 0, day: 0}) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp to_iso8601_duration_date(%{year: year, month: month, week: week, day: day}) do
|
||||
[pair(year, ?Y), pair(month, ?M), pair(week, ?W), pair(day, ?D)]
|
||||
end
|
||||
|
||||
defp to_iso8601_duration_time(%{hour: 0, minute: 0, second: 0, microsecond: {0, _}}) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp to_iso8601_duration_time(%{hour: hour, minute: minute} = d) do
|
||||
[?T, pair(hour, ?H), pair(minute, ?M), second_component(d)]
|
||||
end
|
||||
|
||||
defp second_component(%{second: 0, microsecond: {0, _}}) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp second_component(%{second: 0, microsecond: {_, 0}}) do
|
||||
~c"0S"
|
||||
end
|
||||
|
||||
defp second_component(%{second: second, microsecond: {_, 0}}) do
|
||||
[Integer.to_string(second), ?S]
|
||||
end
|
||||
|
||||
defp second_component(%{second: second, microsecond: {ms, p}}) do
|
||||
total_ms = second * @microseconds_per_second + ms
|
||||
second = total_ms |> div(@microseconds_per_second) |> abs()
|
||||
ms = total_ms |> rem(@microseconds_per_second) |> abs()
|
||||
sign = if total_ms < 0, do: ?-, else: []
|
||||
|
||||
[
|
||||
sign,
|
||||
Integer.to_string(second),
|
||||
?.,
|
||||
ms |> Integer.to_string() |> String.pad_leading(6, "0") |> binary_part(0, p),
|
||||
?S
|
||||
]
|
||||
end
|
||||
|
||||
@compile {:inline, pair: 2}
|
||||
defp pair(0, _key), do: []
|
||||
defp pair(num, key), do: [Integer.to_string(num), key]
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,965 +0,0 @@
|
||||
defmodule Time do
|
||||
@moduledoc """
|
||||
A Time struct and functions.
|
||||
|
||||
The Time struct contains the fields hour, minute, second and microseconds.
|
||||
New times can be built with the `new/4` function or using the
|
||||
`~T` (see `sigil_T/2`) sigil:
|
||||
|
||||
iex> ~T[23:00:07.001]
|
||||
~T[23:00:07.001]
|
||||
|
||||
Both `new/4` and sigil return a struct where the time fields can
|
||||
be accessed directly:
|
||||
|
||||
iex> time = ~T[23:00:07.001]
|
||||
iex> time.hour
|
||||
23
|
||||
iex> time.microsecond
|
||||
{1000, 3}
|
||||
|
||||
The functions on this module work with the `Time` struct as well
|
||||
as any struct that contains the same fields as the `Time` struct,
|
||||
such as `NaiveDateTime` and `DateTime`. Such functions expect
|
||||
`t:Calendar.time/0` in their typespecs (instead of `t:t/0`).
|
||||
|
||||
Developers should avoid creating the Time structs directly
|
||||
and instead rely on the functions provided by this module as well
|
||||
as the ones in third-party calendar libraries.
|
||||
|
||||
## Comparing times
|
||||
|
||||
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
|
||||
and based on the `Time` struct fields. For proper comparison between
|
||||
times, use the `compare/2` function. The existence of the `compare/2`
|
||||
function in this module also allows using `Enum.min/2` and `Enum.max/2`
|
||||
functions to get the minimum and maximum time of an `Enum`. For example:
|
||||
|
||||
iex> Enum.min([~T[23:00:07.001], ~T[10:00:07.001]], Time)
|
||||
~T[10:00:07.001]
|
||||
"""
|
||||
|
||||
@enforce_keys [:hour, :minute, :second]
|
||||
defstruct [:hour, :minute, :second, microsecond: {0, 0}, calendar: Calendar.ISO]
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
hour: Calendar.hour(),
|
||||
minute: Calendar.minute(),
|
||||
second: Calendar.second(),
|
||||
microsecond: Calendar.microsecond(),
|
||||
calendar: Calendar.calendar()
|
||||
}
|
||||
|
||||
@seconds_per_day 24 * 60 * 60
|
||||
|
||||
@doc """
|
||||
Returns the current time in UTC.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> time = Time.utc_now()
|
||||
iex> time.hour >= 0
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec utc_now(Calendar.calendar()) :: t
|
||||
def utc_now(calendar \\ Calendar.ISO) do
|
||||
{:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native)
|
||||
{hour, minute, second} = time
|
||||
|
||||
iso_time = %Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: Calendar.ISO
|
||||
}
|
||||
|
||||
convert!(iso_time, calendar)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a new time.
|
||||
|
||||
Expects all values to be integers. Returns `{:ok, time}` if each
|
||||
entry fits its appropriate range, returns `{:error, reason}` otherwise.
|
||||
|
||||
Microseconds can also be given with a precision, which must be an
|
||||
integer between 0 and 6.
|
||||
|
||||
The built-in calendar does not support leap seconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.new(0, 0, 0, 0)
|
||||
{:ok, ~T[00:00:00.000000]}
|
||||
iex> Time.new(23, 59, 59, 999_999)
|
||||
{:ok, ~T[23:59:59.999999]}
|
||||
|
||||
iex> Time.new(24, 59, 59, 999_999)
|
||||
{:error, :invalid_time}
|
||||
iex> Time.new(23, 60, 59, 999_999)
|
||||
{:error, :invalid_time}
|
||||
iex> Time.new(23, 59, 60, 999_999)
|
||||
{:error, :invalid_time}
|
||||
iex> Time.new(23, 59, 59, 1_000_000)
|
||||
{:error, :invalid_time}
|
||||
|
||||
# Invalid precision
|
||||
Time.new(23, 59, 59, {999_999, 10})
|
||||
{:error, :invalid_time}
|
||||
|
||||
"""
|
||||
@spec new(
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond() | non_neg_integer,
|
||||
Calendar.calendar()
|
||||
) :: {:ok, t} | {:error, atom}
|
||||
def new(hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
|
||||
|
||||
def new(hour, minute, second, microsecond, calendar) when is_integer(microsecond) do
|
||||
new(hour, minute, second, {microsecond, 6}, calendar)
|
||||
end
|
||||
|
||||
def new(hour, minute, second, {microsecond, precision}, calendar)
|
||||
when is_integer(hour) and is_integer(minute) and is_integer(second) and
|
||||
is_integer(microsecond) and is_integer(precision) do
|
||||
case calendar.valid_time?(hour, minute, second, {microsecond, precision}) do
|
||||
true ->
|
||||
time = %Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond, precision},
|
||||
calendar: calendar
|
||||
}
|
||||
|
||||
{:ok, time}
|
||||
|
||||
false ->
|
||||
{:error, :invalid_time}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a new time.
|
||||
|
||||
Expects all values to be integers. Returns `time` if each
|
||||
entry fits its appropriate range, raises if the time is invalid.
|
||||
|
||||
Microseconds can also be given with a precision, which must be an
|
||||
integer between 0 and 6.
|
||||
|
||||
The built-in calendar does not support leap seconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.new!(0, 0, 0, 0)
|
||||
~T[00:00:00.000000]
|
||||
iex> Time.new!(23, 59, 59, 999_999)
|
||||
~T[23:59:59.999999]
|
||||
iex> Time.new!(24, 59, 59, 999_999)
|
||||
** (ArgumentError) cannot build time, reason: :invalid_time
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new!(
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond() | non_neg_integer,
|
||||
Calendar.calendar()
|
||||
) :: t
|
||||
def new!(hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO) do
|
||||
case new(hour, minute, second, microsecond, calendar) do
|
||||
{:ok, time} ->
|
||||
time
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError, "cannot build time, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `time` to a string.
|
||||
|
||||
### Examples
|
||||
|
||||
iex> Time.to_string(~T[23:00:00])
|
||||
"23:00:00"
|
||||
iex> Time.to_string(~T[23:00:00.001])
|
||||
"23:00:00.001"
|
||||
iex> Time.to_string(~T[23:00:00.123456])
|
||||
"23:00:00.123456"
|
||||
|
||||
iex> Time.to_string(~N[2015-01-01 23:00:00.001])
|
||||
"23:00:00.001"
|
||||
iex> Time.to_string(~N[2015-01-01 23:00:00.123456])
|
||||
"23:00:00.123456"
|
||||
|
||||
"""
|
||||
@spec to_string(Calendar.time()) :: String.t()
|
||||
def to_string(time)
|
||||
|
||||
def to_string(%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
}) do
|
||||
calendar.time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Local time" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Time zone offset may be included in the string but they will be
|
||||
simply discarded as such information is not included in times.
|
||||
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_iso8601("23:50:07")
|
||||
{:ok, ~T[23:50:07]}
|
||||
iex> Time.from_iso8601("23:50:07Z")
|
||||
{:ok, ~T[23:50:07]}
|
||||
iex> Time.from_iso8601("T23:50:07Z")
|
||||
{:ok, ~T[23:50:07]}
|
||||
|
||||
iex> Time.from_iso8601("23:50:07,0123456")
|
||||
{:ok, ~T[23:50:07.012345]}
|
||||
iex> Time.from_iso8601("23:50:07.0123456")
|
||||
{:ok, ~T[23:50:07.012345]}
|
||||
iex> Time.from_iso8601("23:50:07.123Z")
|
||||
{:ok, ~T[23:50:07.123]}
|
||||
|
||||
iex> Time.from_iso8601("2015:01:23 23-50-07")
|
||||
{:error, :invalid_format}
|
||||
iex> Time.from_iso8601("23:50:07A")
|
||||
{:error, :invalid_format}
|
||||
iex> Time.from_iso8601("23:50:07.")
|
||||
{:error, :invalid_format}
|
||||
iex> Time.from_iso8601("23:50:61")
|
||||
{:error, :invalid_time}
|
||||
|
||||
"""
|
||||
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO) do
|
||||
with {:ok, {hour, minute, second, microsecond}} <- Calendar.ISO.parse_time(string) do
|
||||
convert(
|
||||
%Time{hour: hour, minute: minute, second: second, microsecond: microsecond},
|
||||
calendar
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Local time" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_iso8601!("23:50:07,123Z")
|
||||
~T[23:50:07.123]
|
||||
iex> Time.from_iso8601!("23:50:07.123Z")
|
||||
~T[23:50:07.123]
|
||||
iex> Time.from_iso8601!("2015:01:23 23-50-07")
|
||||
** (ArgumentError) cannot parse "2015:01:23 23-50-07" as time, reason: :invalid_format
|
||||
|
||||
"""
|
||||
@spec from_iso8601!(String.t(), Calendar.calendar()) :: t
|
||||
def from_iso8601!(string, calendar \\ Calendar.ISO) do
|
||||
case from_iso8601(string, calendar) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError, "cannot parse #{inspect(string)} as time, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given time to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `Time.to_iso8601/2` returns times formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through
|
||||
passing the `:basic` option.
|
||||
|
||||
### Examples
|
||||
|
||||
iex> Time.to_iso8601(~T[23:00:13])
|
||||
"23:00:13"
|
||||
|
||||
iex> Time.to_iso8601(~T[23:00:13.001])
|
||||
"23:00:13.001"
|
||||
|
||||
iex> Time.to_iso8601(~T[23:00:13.001], :basic)
|
||||
"230013.001"
|
||||
|
||||
iex> Time.to_iso8601(~N[2010-04-17 23:00:13])
|
||||
"23:00:13"
|
||||
|
||||
"""
|
||||
@spec to_iso8601(Calendar.time(), :extended | :basic) :: String.t()
|
||||
def to_iso8601(time, format \\ :extended)
|
||||
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = time, format) when format in [:extended, :basic] do
|
||||
%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
} = time
|
||||
|
||||
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = time, format) when format in [:extended, :basic] do
|
||||
time
|
||||
|> convert!(Calendar.ISO)
|
||||
|> to_iso8601(format)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts given `time` to an Erlang time tuple.
|
||||
|
||||
WARNING: Loss of precision may occur, as Erlang time tuples
|
||||
only contain hours/minutes/seconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.to_erl(~T[23:30:15.999])
|
||||
{23, 30, 15}
|
||||
|
||||
iex> Time.to_erl(~N[2010-04-17 23:30:15.999])
|
||||
{23, 30, 15}
|
||||
|
||||
"""
|
||||
@spec to_erl(Calendar.time()) :: :calendar.time()
|
||||
def to_erl(time) do
|
||||
%{hour: hour, minute: minute, second: second} = convert!(time, Calendar.ISO)
|
||||
{hour, minute, second}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an Erlang time tuple to a `Time` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_erl({23, 30, 15}, {5000, 3})
|
||||
{:ok, ~T[23:30:15.005]}
|
||||
iex> Time.from_erl({24, 30, 15})
|
||||
{:error, :invalid_time}
|
||||
|
||||
"""
|
||||
@spec from_erl(:calendar.time(), Calendar.microsecond(), Calendar.calendar()) ::
|
||||
{:ok, t} | {:error, atom}
|
||||
def from_erl(tuple, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
|
||||
|
||||
def from_erl({hour, minute, second}, microsecond, calendar) do
|
||||
with {:ok, time} <- new(hour, minute, second, microsecond, Calendar.ISO),
|
||||
do: convert(time, calendar)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an Erlang time tuple to a `Time` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_erl!({23, 30, 15})
|
||||
~T[23:30:15]
|
||||
iex> Time.from_erl!({23, 30, 15}, {5000, 3})
|
||||
~T[23:30:15.005]
|
||||
iex> Time.from_erl!({24, 30, 15})
|
||||
** (ArgumentError) cannot convert {24, 30, 15} to time, reason: :invalid_time
|
||||
|
||||
"""
|
||||
@spec from_erl!(:calendar.time(), Calendar.microsecond(), Calendar.calendar()) :: t
|
||||
def from_erl!(tuple, microsecond \\ {0, 0}, calendar \\ Calendar.ISO) do
|
||||
case from_erl(tuple, microsecond, calendar) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot convert #{inspect(tuple)} to time, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a number of seconds after midnight to a `Time` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_seconds_after_midnight(10_000)
|
||||
~T[02:46:40]
|
||||
iex> Time.from_seconds_after_midnight(30_000, {5000, 3})
|
||||
~T[08:20:00.005]
|
||||
iex> Time.from_seconds_after_midnight(-1)
|
||||
~T[23:59:59]
|
||||
iex> Time.from_seconds_after_midnight(100_000)
|
||||
~T[03:46:40]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec from_seconds_after_midnight(
|
||||
integer(),
|
||||
Calendar.microsecond(),
|
||||
Calendar.calendar()
|
||||
) :: t
|
||||
def from_seconds_after_midnight(seconds, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
|
||||
when is_integer(seconds) do
|
||||
seconds_in_day = Integer.mod(seconds, @seconds_per_day)
|
||||
|
||||
{hour, minute, second, {_, _}} =
|
||||
calendar.time_from_day_fraction({seconds_in_day, @seconds_per_day})
|
||||
|
||||
%Time{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `Time` struct to a number of seconds after midnight.
|
||||
|
||||
The returned value is a two-element tuple with the number of seconds and microseconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.to_seconds_after_midnight(~T[23:30:15])
|
||||
{84615, 0}
|
||||
iex> Time.to_seconds_after_midnight(~N[2010-04-17 23:30:15.999])
|
||||
{84615, 999000}
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec to_seconds_after_midnight(Calendar.time()) :: {integer(), non_neg_integer()}
|
||||
def to_seconds_after_midnight(%{microsecond: {microsecond, _precision}} = time) do
|
||||
iso_days = {0, to_day_fraction(time)}
|
||||
{Calendar.ISO.iso_days_to_unit(iso_days, :second), microsecond}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Adds the `amount_to_add` of `unit`s to the given `time`.
|
||||
|
||||
Accepts an `amount_to_add` in any `unit`. `unit` can be
|
||||
`:hour`, `:minute`, `:second` or any subsecond precision from
|
||||
`t:System.time_unit/0`. It defaults to `:second`. Negative values
|
||||
will move backwards in time.
|
||||
|
||||
This function always consider the unit to be computed according
|
||||
to the `Calendar.ISO`.
|
||||
|
||||
Note the result value represents the time of day, meaning that it is cyclic,
|
||||
for instance, it will never go over 24 hours for the ISO calendar.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.add(~T[10:00:00], 27000)
|
||||
~T[17:30:00]
|
||||
iex> Time.add(~T[11:00:00.005], 2400)
|
||||
~T[11:40:00.005]
|
||||
iex> Time.add(~T[00:00:00.000], 86_399_999, :millisecond)
|
||||
~T[23:59:59.999]
|
||||
|
||||
Negative values are allowed:
|
||||
|
||||
iex> Time.add(~T[23:00:00], -60)
|
||||
~T[22:59:00]
|
||||
|
||||
Note that the time is cyclic:
|
||||
|
||||
iex> Time.add(~T[17:10:05], 86400)
|
||||
~T[17:10:05]
|
||||
|
||||
Hours and minutes are also supported:
|
||||
|
||||
iex> Time.add(~T[17:10:05], 2, :hour)
|
||||
~T[19:10:05]
|
||||
iex> Time.add(~T[17:10:05], 30, :minute)
|
||||
~T[17:40:05]
|
||||
|
||||
This operation merges the precision of the time with the given unit:
|
||||
|
||||
iex> result = Time.add(~T[00:29:10], 21, :millisecond)
|
||||
~T[00:29:10.021]
|
||||
iex> result.microsecond
|
||||
{21000, 3}
|
||||
|
||||
To shift a time by a `Duration` and according to its underlying calendar, use `Time.shift/2`.
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec add(Calendar.time(), integer, :hour | :minute | System.time_unit()) :: t
|
||||
def add(time, amount_to_add, unit \\ :second)
|
||||
|
||||
def add(time, amount_to_add, :hour) when is_integer(amount_to_add) do
|
||||
add(time, amount_to_add * 3600, :second)
|
||||
end
|
||||
|
||||
def add(time, amount_to_add, :minute) when is_integer(amount_to_add) do
|
||||
add(time, amount_to_add * 60, :second)
|
||||
end
|
||||
|
||||
def add(%{calendar: calendar, microsecond: {_, precision}} = time, amount_to_add, unit)
|
||||
when is_integer(amount_to_add) do
|
||||
valid? =
|
||||
if is_integer(unit),
|
||||
do: unit > 0,
|
||||
else: unit in ~w(second millisecond microsecond nanosecond)a
|
||||
|
||||
unless valid? do
|
||||
raise ArgumentError,
|
||||
"unsupported time unit. Expected :hour, :minute, :second, :millisecond, :microsecond, :nanosecond, or a positive integer, got #{inspect(unit)}"
|
||||
end
|
||||
|
||||
%{hour: hour, minute: minute, second: second, microsecond: microsecond} = time
|
||||
|
||||
precision = max(Calendar.ISO.time_unit_to_precision(unit), precision)
|
||||
|
||||
{hour, minute, second, {microsecond, _precision}} =
|
||||
Calendar.ISO.shift_time_unit(
|
||||
{hour, minute, second, microsecond},
|
||||
amount_to_add,
|
||||
unit
|
||||
)
|
||||
|
||||
%Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond, precision},
|
||||
calendar: calendar
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Shifts given `time` by `duration` according to its calendar.
|
||||
|
||||
Available duration units are: `:hour`, `:minute`, `:second`, `:microsecond`.
|
||||
|
||||
When using the default ISO calendar, durations are collapsed to seconds and
|
||||
microseconds before they are applied.
|
||||
|
||||
Raises an `ArgumentError` when called with date scale units.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.shift(~T[01:00:15], hour: 12)
|
||||
~T[13:00:15]
|
||||
iex> Time.shift(~T[01:35:00], hour: 6, minute: -15)
|
||||
~T[07:20:00]
|
||||
iex> Time.shift(~T[01:15:00], second: 125)
|
||||
~T[01:17:05]
|
||||
iex> Time.shift(~T[01:00:15], microsecond: {100, 6})
|
||||
~T[01:00:15.000100]
|
||||
iex> Time.shift(~T[01:15:00], Duration.new!(second: 65))
|
||||
~T[01:16:05]
|
||||
|
||||
"""
|
||||
@doc since: "1.17.0"
|
||||
@spec shift(Calendar.time(), Duration.t() | [unit_pair]) :: t
|
||||
when unit_pair:
|
||||
{:hour, integer}
|
||||
| {:minute, integer}
|
||||
| {:second, integer}
|
||||
| {:microsecond, {integer, 0..6}}
|
||||
def shift(%{calendar: calendar} = time, duration) do
|
||||
%{hour: hour, minute: minute, second: second, microsecond: microsecond} = time
|
||||
|
||||
{hour, minute, second, microsecond} =
|
||||
calendar.shift_time(hour, minute, second, microsecond, __duration__!(duration))
|
||||
|
||||
%Time{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __duration__!(%Duration{} = duration) do
|
||||
duration
|
||||
end
|
||||
|
||||
# This part is inlined by the compiler on constant values
|
||||
def __duration__!(unit_pairs) do
|
||||
Enum.each(unit_pairs, &validate_duration_unit!/1)
|
||||
struct!(Duration, unit_pairs)
|
||||
end
|
||||
|
||||
defp validate_duration_unit!({:microsecond, {ms, precision}})
|
||||
when is_integer(ms) and precision in 0..6 do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp validate_duration_unit!({:microsecond, microsecond}) do
|
||||
raise ArgumentError,
|
||||
"unsupported value #{inspect(microsecond)} for :microsecond. Expected a tuple {ms, precision} where precision is an integer from 0 to 6"
|
||||
end
|
||||
|
||||
defp validate_duration_unit!({unit, _value}) when unit in [:year, :month, :week, :day] do
|
||||
raise ArgumentError,
|
||||
"unsupported unit #{inspect(unit)}. Expected :hour, :minute, :second, :microsecond"
|
||||
end
|
||||
|
||||
defp validate_duration_unit!({unit, _value})
|
||||
when unit not in [:hour, :minute, :second, :microsecond] do
|
||||
raise ArgumentError,
|
||||
"unknown unit #{inspect(unit)}. Expected :hour, :minute, :second, :microsecond"
|
||||
end
|
||||
|
||||
defp validate_duration_unit!({_unit, value}) when is_integer(value) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp validate_duration_unit!({unit, value}) do
|
||||
raise ArgumentError,
|
||||
"unsupported value #{inspect(value)} for #{inspect(unit)}. Expected an integer"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compares two time structs.
|
||||
|
||||
Returns `:gt` if first time is later than the second
|
||||
and `:lt` for vice versa. If the two times are equal
|
||||
`:eq` is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.compare(~T[16:04:16], ~T[16:04:28])
|
||||
:lt
|
||||
iex> Time.compare(~T[16:04:16], ~T[16:04:16])
|
||||
:eq
|
||||
iex> Time.compare(~T[16:04:16.01], ~T[16:04:16.001])
|
||||
:gt
|
||||
|
||||
This function can also be used to compare across more
|
||||
complex calendar types by considering only the time fields:
|
||||
|
||||
iex> Time.compare(~N[1900-01-01 16:04:16], ~N[2015-01-01 16:04:16])
|
||||
:eq
|
||||
iex> Time.compare(~N[2015-01-01 16:04:16], ~N[2015-01-01 16:04:28])
|
||||
:lt
|
||||
iex> Time.compare(~N[2015-01-01 16:04:16.01], ~N[2000-01-01 16:04:16.001])
|
||||
:gt
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec compare(Calendar.time(), Calendar.time()) :: :lt | :eq | :gt
|
||||
def compare(%{calendar: calendar} = time1, %{calendar: calendar} = time2) do
|
||||
%{hour: hour1, minute: minute1, second: second1, microsecond: {microsecond1, _}} = time1
|
||||
%{hour: hour2, minute: minute2, second: second2, microsecond: {microsecond2, _}} = time2
|
||||
|
||||
case {{hour1, minute1, second1, microsecond1}, {hour2, minute2, second2, microsecond2}} do
|
||||
{first, second} when first > second -> :gt
|
||||
{first, second} when first < second -> :lt
|
||||
_ -> :eq
|
||||
end
|
||||
end
|
||||
|
||||
def compare(time1, time2) do
|
||||
{parts1, ppd1} = to_day_fraction(time1)
|
||||
{parts2, ppd2} = to_day_fraction(time2)
|
||||
|
||||
case {parts1 * ppd2, parts2 * ppd1} do
|
||||
{first, second} when first > second -> :gt
|
||||
{first, second} when first < second -> :lt
|
||||
_ -> :eq
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the first time is strictly earlier than the second.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.before?(~T[16:04:16], ~T[16:04:28])
|
||||
true
|
||||
iex> Time.before?(~T[16:04:16], ~T[16:04:16])
|
||||
false
|
||||
iex> Time.before?(~T[16:04:16.01], ~T[16:04:16.001])
|
||||
false
|
||||
|
||||
"""
|
||||
@doc since: "1.15.0"
|
||||
@spec before?(Calendar.time(), Calendar.time()) :: boolean()
|
||||
def before?(time1, time2) do
|
||||
compare(time1, time2) == :lt
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the first time is strictly later than the second.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.after?(~T[16:04:28], ~T[16:04:16])
|
||||
true
|
||||
iex> Time.after?(~T[16:04:16], ~T[16:04:16])
|
||||
false
|
||||
iex> Time.after?(~T[16:04:16.001], ~T[16:04:16.01])
|
||||
false
|
||||
|
||||
"""
|
||||
@doc since: "1.15.0"
|
||||
@spec after?(Calendar.time(), Calendar.time()) :: boolean()
|
||||
def after?(time1, time2) do
|
||||
compare(time1, time2) == :gt
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts given `time` to a different calendar.
|
||||
|
||||
Returns `{:ok, time}` if the conversion was successful,
|
||||
or `{:error, reason}` if it was not, for some reason.
|
||||
|
||||
## Examples
|
||||
|
||||
Imagine someone implements `Calendar.Holocene`, a calendar based on the
|
||||
Gregorian calendar that adds exactly 10,000 years to the current Gregorian
|
||||
year:
|
||||
|
||||
iex> Time.convert(~T[13:30:15], Calendar.Holocene)
|
||||
{:ok, %Time{calendar: Calendar.Holocene, hour: 13, minute: 30, second: 15, microsecond: {0, 0}}}
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec convert(Calendar.time(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
|
||||
|
||||
# Keep it multiline for proper function clause errors.
|
||||
def convert(
|
||||
%{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
},
|
||||
calendar
|
||||
) do
|
||||
time = %Time{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}
|
||||
|
||||
{:ok, time}
|
||||
end
|
||||
|
||||
def convert(%{microsecond: {_, precision}} = time, calendar) do
|
||||
{hour, minute, second, {microsecond, _}} =
|
||||
time
|
||||
|> to_day_fraction()
|
||||
|> calendar.time_from_day_fraction()
|
||||
|
||||
time = %Time{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond, precision}
|
||||
}
|
||||
|
||||
{:ok, time}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `Time.convert/2`, but raises an `ArgumentError`
|
||||
if the conversion between the two calendars is not possible.
|
||||
|
||||
## Examples
|
||||
|
||||
Imagine someone implements `Calendar.Holocene`, a calendar based on the
|
||||
Gregorian calendar that adds exactly 10,000 years to the current Gregorian
|
||||
year:
|
||||
|
||||
iex> Time.convert!(~T[13:30:15], Calendar.Holocene)
|
||||
%Time{calendar: Calendar.Holocene, hour: 13, minute: 30, second: 15, microsecond: {0, 0}}
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec convert!(Calendar.time(), Calendar.calendar()) :: t
|
||||
def convert!(time, calendar) do
|
||||
case convert(time, calendar) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot convert #{inspect(time)} to target calendar #{inspect(calendar)}, " <>
|
||||
"reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the difference between two times, considering only the hour, minute,
|
||||
second and microsecond.
|
||||
|
||||
As with the `compare/2` function both `Time` structs and other structures
|
||||
containing time can be used. If for instance a `NaiveDateTime` or `DateTime`
|
||||
is passed, only the hour, minute, second, and microsecond is considered. Any
|
||||
additional information about a date or time zone is ignored when calculating
|
||||
the difference.
|
||||
|
||||
The answer can be returned in any `:hour`, `:minute`, `:second` or any
|
||||
subsecond `unit` available from `t:System.time_unit/0`. If the first time
|
||||
value is earlier than the second, a negative number is returned.
|
||||
|
||||
The unit is measured according to `Calendar.ISO` and defaults to `:second`.
|
||||
Fractional results are not supported and are truncated.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.diff(~T[00:29:12], ~T[00:29:10])
|
||||
2
|
||||
|
||||
# When passing a `NaiveDateTime` the date part is ignored.
|
||||
iex> Time.diff(~N[2017-01-01 00:29:12], ~T[00:29:10])
|
||||
2
|
||||
|
||||
# Two `NaiveDateTime` structs could have big differences in the date
|
||||
# but only the time part is considered.
|
||||
iex> Time.diff(~N[2017-01-01 00:29:12], ~N[1900-02-03 00:29:10])
|
||||
2
|
||||
|
||||
iex> Time.diff(~T[00:29:12], ~T[00:29:10], :microsecond)
|
||||
2_000_000
|
||||
iex> Time.diff(~T[00:29:10], ~T[00:29:12], :microsecond)
|
||||
-2_000_000
|
||||
|
||||
iex> Time.diff(~T[02:29:10], ~T[00:29:10], :hour)
|
||||
2
|
||||
iex> Time.diff(~T[02:29:10], ~T[00:29:11], :hour)
|
||||
1
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec diff(Calendar.time(), Calendar.time(), :hour | :minute | System.time_unit()) :: integer
|
||||
def diff(time1, time2, unit \\ :second)
|
||||
|
||||
def diff(time1, time2, :hour) do
|
||||
diff(time1, time2, :second) |> div(3600)
|
||||
end
|
||||
|
||||
def diff(time1, time2, :minute) do
|
||||
diff(time1, time2, :second) |> div(60)
|
||||
end
|
||||
|
||||
def diff(
|
||||
%{
|
||||
calendar: Calendar.ISO,
|
||||
hour: hour1,
|
||||
minute: minute1,
|
||||
second: second1,
|
||||
microsecond: {microsecond1, _}
|
||||
},
|
||||
%{
|
||||
calendar: Calendar.ISO,
|
||||
hour: hour2,
|
||||
minute: minute2,
|
||||
second: second2,
|
||||
microsecond: {microsecond2, _}
|
||||
},
|
||||
unit
|
||||
) do
|
||||
total =
|
||||
(hour1 - hour2) * 3_600_000_000 + (minute1 - minute2) * 60_000_000 +
|
||||
(second1 - second2) * 1_000_000 + (microsecond1 - microsecond2)
|
||||
|
||||
System.convert_time_unit(total, :microsecond, unit)
|
||||
end
|
||||
|
||||
def diff(time1, time2, unit) do
|
||||
fraction1 = to_day_fraction(time1)
|
||||
fraction2 = to_day_fraction(time2)
|
||||
|
||||
Calendar.ISO.iso_days_to_unit({0, fraction1}, unit) -
|
||||
Calendar.ISO.iso_days_to_unit({0, fraction2}, unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the given time with the microsecond field truncated to the given
|
||||
precision (`:microsecond`, `millisecond` or `:second`).
|
||||
|
||||
The given time is returned unchanged if it already has lower precision than
|
||||
the given precision.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.truncate(~T[01:01:01.123456], :microsecond)
|
||||
~T[01:01:01.123456]
|
||||
|
||||
iex> Time.truncate(~T[01:01:01.123456], :millisecond)
|
||||
~T[01:01:01.123]
|
||||
|
||||
iex> Time.truncate(~T[01:01:01.123456], :second)
|
||||
~T[01:01:01]
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec truncate(t(), :microsecond | :millisecond | :second) :: t()
|
||||
def truncate(%Time{microsecond: microsecond} = time, precision) do
|
||||
%{time | microsecond: Calendar.truncate(microsecond, precision)}
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp to_day_fraction(%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {_, _} = microsecond,
|
||||
calendar: calendar
|
||||
}) do
|
||||
calendar.time_to_day_fraction(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
defimpl String.Chars do
|
||||
def to_string(time) do
|
||||
%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
} = time
|
||||
|
||||
calendar.time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
def inspect(time, _) do
|
||||
%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
} = time
|
||||
|
||||
"~T[" <>
|
||||
calendar.time_to_string(hour, minute, second, microsecond) <> suffix(calendar) <> "]"
|
||||
end
|
||||
|
||||
defp suffix(Calendar.ISO), do: ""
|
||||
defp suffix(calendar), do: " " <> inspect(calendar)
|
||||
end
|
||||
end
|
||||
@@ -1,98 +0,0 @@
|
||||
defmodule Calendar.TimeZoneDatabase do
|
||||
@moduledoc """
|
||||
This module defines a behaviour for providing time zone data.
|
||||
|
||||
IANA provides time zone data that includes data about different
|
||||
UTC offsets and standard offsets for time zones.
|
||||
"""
|
||||
|
||||
@typedoc """
|
||||
A period where a certain combination of UTC offset, standard offset, and zone
|
||||
abbreviation is in effect.
|
||||
|
||||
For example, one period could be the summer of 2018 in the `Europe/London` timezone,
|
||||
where summer time/daylight saving time is in effect and lasts from spring to autumn.
|
||||
In autumn, the `std_offset` changes along with the `zone_abbr` so a different
|
||||
period is needed during winter.
|
||||
"""
|
||||
@type time_zone_period :: %{
|
||||
optional(any) => any,
|
||||
utc_offset: Calendar.utc_offset(),
|
||||
std_offset: Calendar.std_offset(),
|
||||
zone_abbr: Calendar.zone_abbr()
|
||||
}
|
||||
|
||||
@typedoc """
|
||||
Limit for when a certain time zone period begins or ends.
|
||||
|
||||
A beginning is inclusive. An ending is exclusive. For example, if a period is from
|
||||
`2015-03-29 01:00:00` and until `2015-10-25 01:00:00`, the period includes and
|
||||
begins from the beginning of `2015-03-29 01:00:00` and lasts until just before
|
||||
`2015-10-25 01:00:00`.
|
||||
|
||||
A beginning or end for certain periods are infinite, such as the latest
|
||||
period for time zones without DST or plans to change. However, for the purpose
|
||||
of this behaviour, they are only used for gaps in wall time where the needed
|
||||
period limits are at a certain time.
|
||||
"""
|
||||
@type time_zone_period_limit :: Calendar.naive_datetime()
|
||||
|
||||
@doc """
|
||||
Time zone period for a point in time in UTC for a specific time zone.
|
||||
|
||||
Takes a time zone name and a point in time for UTC and returns a
|
||||
`time_zone_period` for that point in time.
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@callback time_zone_period_from_utc_iso_days(Calendar.iso_days(), Calendar.time_zone()) ::
|
||||
{:ok, time_zone_period}
|
||||
| {:error, :time_zone_not_found | :utc_only_time_zone_database}
|
||||
|
||||
@doc """
|
||||
Possible time zone periods for a certain time zone and wall clock date and time.
|
||||
|
||||
When the provided naive datetime is ambiguous, return a tuple with `:ambiguous`
|
||||
and the two possible periods. The periods in the tuple must be sorted with the
|
||||
first element being the one that begins first.
|
||||
|
||||
When the provided naive datetime is in a gap, such as during the "spring forward" when going
|
||||
from winter time to summer time, return a tuple with `:gap` and two periods with limits
|
||||
in a nested tuple. The first nested two-tuple is the period before the gap and a naive datetime
|
||||
with a limit for when the period ends (wall time). The second nested two-tuple is the period
|
||||
just after the gap and a datetime (wall time) for when the period begins just after the gap.
|
||||
|
||||
If there is only a single possible period for the provided `datetime`, then return a tuple
|
||||
with `:ok` and the `time_zone_period`.
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@callback time_zone_periods_from_wall_datetime(Calendar.naive_datetime(), Calendar.time_zone()) ::
|
||||
{:ok, time_zone_period}
|
||||
| {:ambiguous, time_zone_period, time_zone_period}
|
||||
| {:gap, {time_zone_period, time_zone_period_limit},
|
||||
{time_zone_period, time_zone_period_limit}}
|
||||
| {:error, :time_zone_not_found | :utc_only_time_zone_database}
|
||||
end
|
||||
|
||||
defmodule Calendar.UTCOnlyTimeZoneDatabase do
|
||||
@moduledoc """
|
||||
Built-in time zone database that works only in the `Etc/UTC` timezone.
|
||||
|
||||
For all other time zones, it returns `{:error, :utc_only_time_zone_database}`.
|
||||
"""
|
||||
|
||||
@behaviour Calendar.TimeZoneDatabase
|
||||
|
||||
@impl true
|
||||
def time_zone_period_from_utc_iso_days(_, "Etc/UTC"),
|
||||
do: {:ok, %{std_offset: 0, utc_offset: 0, zone_abbr: "UTC"}}
|
||||
|
||||
def time_zone_period_from_utc_iso_days(_, _),
|
||||
do: {:error, :utc_only_time_zone_database}
|
||||
|
||||
@impl true
|
||||
def time_zone_periods_from_wall_datetime(_, "Etc/UTC"),
|
||||
do: {:ok, %{std_offset: 0, utc_offset: 0, zone_abbr: "UTC"}}
|
||||
|
||||
def time_zone_periods_from_wall_datetime(_, _),
|
||||
do: {:error, :utc_only_time_zone_database}
|
||||
end
|
||||
+462
-2064
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,152 +0,0 @@
|
||||
defmodule Code.Identifier do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Checks if the given identifier is an unary op.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Identifier.unary_op(:+)
|
||||
{:non_associative, 300}
|
||||
|
||||
"""
|
||||
@spec unary_op(atom) :: {:non_associative, precedence :: pos_integer} | :error
|
||||
def unary_op(op) do
|
||||
cond do
|
||||
op in [:&, :...] -> {:non_associative, 90}
|
||||
op in [:!, :^, :not, :+, :-, :"~~~"] -> {:non_associative, 300}
|
||||
op in [:@] -> {:non_associative, 320}
|
||||
true -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given identifier is a binary op.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Identifier.binary_op(:+)
|
||||
{:left, 210}
|
||||
|
||||
"""
|
||||
@spec binary_op(atom) :: {:left | :right, precedence :: pos_integer} | :error
|
||||
def binary_op(op) do
|
||||
cond do
|
||||
op in [:<-, :\\] -> {:left, 40}
|
||||
op in [:when] -> {:right, 50}
|
||||
op in [:"::"] -> {:right, 60}
|
||||
op in [:|] -> {:right, 70}
|
||||
op in [:=] -> {:right, 100}
|
||||
op in [:||, :|||, :or] -> {:left, 120}
|
||||
op in [:&&, :&&&, :and] -> {:left, 130}
|
||||
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
|
||||
op in [:<, :<=, :>=, :>] -> {:left, 150}
|
||||
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :"<|>"] -> {:left, 160}
|
||||
op in [:in] -> {:left, 170}
|
||||
op in [:"^^^"] -> {:left, 180}
|
||||
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
|
||||
op in [:+, :-] -> {:left, 210}
|
||||
op in [:*, :/] -> {:left, 220}
|
||||
op in [:**] -> {:left, 230}
|
||||
op in [:.] -> {:left, 310}
|
||||
true -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Extracts the name and arity of the parent from the anonymous function identifier.
|
||||
"""
|
||||
# Example of this format: -NAME/ARITY-fun-COUNT-
|
||||
def extract_anonymous_fun_parent(atom) when is_atom(atom) do
|
||||
with "-" <> rest <- Atom.to_string(atom),
|
||||
[trailing | reversed] = rest |> String.split("/") |> Enum.reverse(),
|
||||
[arity, _inner, _count, ""] <- String.split(trailing, "-") do
|
||||
{reversed |> Enum.reverse() |> Enum.join("/") |> String.to_atom(), arity}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Escapes the given identifier.
|
||||
"""
|
||||
@spec escape(binary(), char() | nil, :infinity | non_neg_integer, (char() -> iolist() | false)) ::
|
||||
{escaped :: iolist(), remaining :: binary()}
|
||||
def escape(binary, char, limit \\ :infinity, fun \\ &escape_map/1)
|
||||
when ((char in 0..0x10FFFF or is_nil(char)) and limit == :infinity) or
|
||||
(is_integer(limit) and limit >= 0) do
|
||||
escape(binary, char, limit, [], fun)
|
||||
end
|
||||
|
||||
defp escape(<<_, _::binary>> = binary, _char, 0, acc, _fun) do
|
||||
{acc, binary}
|
||||
end
|
||||
|
||||
defp escape(<<char, t::binary>>, char, count, acc, fun) do
|
||||
escape(t, char, decrement(count), [acc | [?\\, char]], fun)
|
||||
end
|
||||
|
||||
defp escape(<<?#, ?{, t::binary>>, char, count, acc, fun) do
|
||||
escape(t, char, decrement(count), [acc | [?\\, ?#, ?{]], fun)
|
||||
end
|
||||
|
||||
defp escape(<<h::utf8, t::binary>>, char, count, acc, fun) do
|
||||
escaped = if value = fun.(h), do: value, else: escape_char(h)
|
||||
escape(t, char, decrement(count), [acc | escaped], fun)
|
||||
end
|
||||
|
||||
defp escape(<<a::4, b::4, t::binary>>, char, count, acc, fun) do
|
||||
escape(t, char, decrement(count), [acc | [?\\, ?x, to_hex(a), to_hex(b)]], fun)
|
||||
end
|
||||
|
||||
defp escape(<<>>, _char, _count, acc, _fun) do
|
||||
{acc, <<>>}
|
||||
end
|
||||
|
||||
defp escape_char(0), do: [?\\, ?0]
|
||||
|
||||
@escaped_bom :binary.bin_to_list("\\uFEFF")
|
||||
defp escape_char(65279), do: @escaped_bom
|
||||
|
||||
defp escape_char(char)
|
||||
when char in 0x20..0x7E
|
||||
when char in 0xA0..0xD7FF
|
||||
when char in 0xE000..0xFFFD
|
||||
when char in 0x10000..0x10FFFF do
|
||||
<<char::utf8>>
|
||||
end
|
||||
|
||||
defp escape_char(char) when char < 0x100 do
|
||||
<<a::4, b::4>> = <<char::8>>
|
||||
[?\\, ?x, to_hex(a), to_hex(b)]
|
||||
end
|
||||
|
||||
defp escape_char(char) when char < 0x10000 do
|
||||
<<a::4, b::4, c::4, d::4>> = <<char::16>>
|
||||
[?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c), to_hex(d), ?}]
|
||||
end
|
||||
|
||||
defp escape_char(char) when char < 0x1000000 do
|
||||
<<a::4, b::4, c::4, d::4, e::4, f::4>> = <<char::24>>
|
||||
[?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c), to_hex(d), to_hex(e), to_hex(f), ?}]
|
||||
end
|
||||
|
||||
defp escape_map(?\a), do: [?\\, ?a]
|
||||
defp escape_map(?\b), do: [?\\, ?b]
|
||||
defp escape_map(?\d), do: [?\\, ?d]
|
||||
defp escape_map(?\e), do: [?\\, ?e]
|
||||
defp escape_map(?\f), do: [?\\, ?f]
|
||||
defp escape_map(?\n), do: [?\\, ?n]
|
||||
defp escape_map(?\r), do: [?\\, ?r]
|
||||
defp escape_map(?\t), do: [?\\, ?t]
|
||||
defp escape_map(?\v), do: [?\\, ?v]
|
||||
defp escape_map(?\\), do: [?\\, ?\\]
|
||||
defp escape_map(_), do: false
|
||||
|
||||
@compile {:inline, to_hex: 1, decrement: 1}
|
||||
defp to_hex(c) when c in 0..9, do: ?0 + c
|
||||
defp to_hex(c) when c in 10..15, do: ?A + c - 10
|
||||
|
||||
defp decrement(:infinity), do: :infinity
|
||||
defp decrement(counter), do: counter - 1
|
||||
end
|
||||
@@ -1,595 +0,0 @@
|
||||
defmodule Code.Normalizer do
|
||||
@moduledoc false
|
||||
|
||||
defguard is_literal(x)
|
||||
when is_integer(x) or
|
||||
is_float(x) or
|
||||
is_binary(x) or
|
||||
is_atom(x)
|
||||
|
||||
@doc """
|
||||
Wraps literals in the quoted expression to conform to the AST format expected
|
||||
by the formatter.
|
||||
"""
|
||||
def normalize(quoted, opts \\ []) do
|
||||
line = Keyword.get(opts, :line, nil)
|
||||
escape = Keyword.get(opts, :escape, true)
|
||||
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
|
||||
|
||||
state = %{
|
||||
escape: escape,
|
||||
parent_meta: [line: line],
|
||||
locals_without_parens: locals_without_parens ++ Code.Formatter.locals_without_parens()
|
||||
}
|
||||
|
||||
do_normalize(quoted, state)
|
||||
end
|
||||
|
||||
# Wrapped literals should receive the block meta
|
||||
defp do_normalize({:__block__, meta, [literal]}, state)
|
||||
when not is_tuple(literal) or tuple_size(literal) == 2 do
|
||||
normalize_literal(literal, meta, state)
|
||||
end
|
||||
|
||||
# Only normalize the first argument of an alias if it's not an atom
|
||||
defp do_normalize({:__aliases__, meta, [first | rest]}, state) when not is_atom(first) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
first = do_normalize(first, %{state | parent_meta: meta})
|
||||
{:__aliases__, meta, [first | rest]}
|
||||
end
|
||||
|
||||
defp do_normalize({:__aliases__, _, _} = quoted, _state) do
|
||||
quoted
|
||||
end
|
||||
|
||||
# Skip captured arguments like &1
|
||||
defp do_normalize({:&, meta, [term]}, state) when is_integer(term) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
{:&, meta, [term]}
|
||||
end
|
||||
|
||||
# Ranges
|
||||
defp do_normalize(left..right//step, state) do
|
||||
left = do_normalize(left, state)
|
||||
right = do_normalize(right, state)
|
||||
meta = meta_line(state)
|
||||
|
||||
if step == 1 do
|
||||
{:.., meta, [left, right]}
|
||||
else
|
||||
step = do_normalize(step, state)
|
||||
{:"..//", meta, [left, right, step]}
|
||||
end
|
||||
end
|
||||
|
||||
# Bit containers
|
||||
defp do_normalize({:<<>>, _, args} = quoted, state) when is_list(args) do
|
||||
normalize_bitstring(quoted, state)
|
||||
end
|
||||
|
||||
# Atoms with interpolations
|
||||
defp do_normalize(
|
||||
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
|
||||
[{:<<>>, _, parts} = string, :utf8]},
|
||||
state
|
||||
)
|
||||
when is_list(parts) do
|
||||
dot_meta = patch_meta_line(dot_meta, state.parent_meta)
|
||||
call_meta = patch_meta_line(call_meta, dot_meta)
|
||||
|
||||
utf8 =
|
||||
if parts == [] or binary_interpolated?(parts) do
|
||||
# a non-normalized :utf8 atom signals an atom interpolation
|
||||
:utf8
|
||||
else
|
||||
normalize_literal(:utf8, [], state)
|
||||
end
|
||||
|
||||
string =
|
||||
if state.escape do
|
||||
normalize_bitstring(string, state, true)
|
||||
else
|
||||
normalize_bitstring(string, state)
|
||||
end
|
||||
|
||||
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, utf8]}
|
||||
end
|
||||
|
||||
# Charlists with interpolations
|
||||
# TODO: Remove this clause on Elixir v2.0 once single-quoted charlists are removed
|
||||
defp do_normalize({{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]} = quoted, state) do
|
||||
if list_interpolated?(parts) do
|
||||
parts =
|
||||
Enum.map(parts, fn
|
||||
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args} ->
|
||||
args = normalize_args(args, state)
|
||||
|
||||
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args}
|
||||
|
||||
part when is_binary(part) ->
|
||||
if state.escape do
|
||||
maybe_escape_literal(part, state)
|
||||
else
|
||||
part
|
||||
end
|
||||
end)
|
||||
|
||||
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
|
||||
else
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Don't normalize the `Access` atom in access syntax
|
||||
defp do_normalize({:., meta, [Access, :get]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
{:., meta, [Access, :get]}
|
||||
end
|
||||
|
||||
# Only normalize the left side of the dot operator
|
||||
# The right hand side is an atom in the AST but it's not an atom literal, so
|
||||
# it should not be wrapped
|
||||
defp do_normalize({:., meta, [left, right]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
left = do_normalize(left, %{state | parent_meta: meta})
|
||||
|
||||
{:., meta, [left, right]}
|
||||
end
|
||||
|
||||
# left -> right
|
||||
defp do_normalize({:->, meta, [left, right]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
left = normalize_args(left, %{state | parent_meta: meta})
|
||||
right = do_normalize(right, %{state | parent_meta: meta})
|
||||
{:->, meta, [left, right]}
|
||||
end
|
||||
|
||||
# Maps
|
||||
defp do_normalize({:%{}, meta, args}, state) when is_list(args) do
|
||||
meta =
|
||||
if meta == [] do
|
||||
line = state.parent_meta[:line]
|
||||
[line: line, closing: [line: line]]
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
args =
|
||||
case args do
|
||||
[{:|, pipe_meta, [left, right]}] ->
|
||||
left = do_normalize(left, state)
|
||||
right = normalize_map_args(right, state)
|
||||
[{:|, pipe_meta, [left, right]}]
|
||||
|
||||
args ->
|
||||
normalize_map_args(args, state)
|
||||
end
|
||||
|
||||
{:%{}, meta, args}
|
||||
end
|
||||
|
||||
# Sigils
|
||||
defp do_normalize({sigil, meta, [{:<<>>, _, args} = string, modifiers]} = quoted, state)
|
||||
when is_list(args) and is_atom(sigil) do
|
||||
case Atom.to_string(sigil) do
|
||||
"sigil_" <> _ ->
|
||||
meta =
|
||||
meta
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|> Keyword.put_new(:delimiter, "\"")
|
||||
|
||||
{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
|
||||
|
||||
_ ->
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Tuples
|
||||
defp do_normalize({:{}, meta, args} = quoted, state) when is_list(args) do
|
||||
{last_arg, args} = List.pop_at(args, -1)
|
||||
|
||||
if args != [] and match?([_ | _], last_arg) and keyword?(last_arg) do
|
||||
args = normalize_args(args, state)
|
||||
kw_list = normalize_kw_args(last_arg, state, true)
|
||||
{:{}, meta, args ++ kw_list}
|
||||
else
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Module attributes
|
||||
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
|
||||
value =
|
||||
cond do
|
||||
keyword?(value) and value != [] ->
|
||||
normalize_kw_args(value, state, true)
|
||||
|
||||
is_list(value) ->
|
||||
normalize_literal(value, meta, state)
|
||||
|
||||
true ->
|
||||
do_normalize(value, state)
|
||||
end
|
||||
|
||||
{:@, meta, [{name, name_meta, [value]}]}
|
||||
end
|
||||
|
||||
# Regular blocks
|
||||
defp do_normalize({:__block__, meta, args}, state) when is_list(args) do
|
||||
{:__block__, meta, normalize_args(args, state)}
|
||||
end
|
||||
|
||||
# Calls
|
||||
defp do_normalize({_, _, args} = quoted, state) when is_list(args) do
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
|
||||
# Vars
|
||||
defp do_normalize({_, _, context} = quoted, _state) when is_atom(context) do
|
||||
quoted
|
||||
end
|
||||
|
||||
# Literals
|
||||
defp do_normalize(quoted, state) do
|
||||
normalize_literal(quoted, [], state)
|
||||
end
|
||||
|
||||
# Numbers
|
||||
defp normalize_literal(number, meta, state) when is_number(number) do
|
||||
meta =
|
||||
meta
|
||||
|> Keyword.put_new(:token, inspect(number))
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|
||||
{:__block__, meta, [number]}
|
||||
end
|
||||
|
||||
# Atom, Strings
|
||||
defp normalize_literal(literal, meta, state) when is_atom(literal) or is_binary(literal) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
literal = maybe_escape_literal(literal, state)
|
||||
|
||||
if is_atom(literal) and Macro.classify_atom(literal) == :alias and
|
||||
is_nil(meta[:delimiter]) do
|
||||
segments =
|
||||
case Atom.to_string(literal) do
|
||||
"Elixir" ->
|
||||
[:"Elixir"]
|
||||
|
||||
"Elixir." <> segments ->
|
||||
segments
|
||||
|> String.split(".")
|
||||
|> Enum.map(&String.to_atom/1)
|
||||
end
|
||||
|
||||
{:__aliases__, meta, segments}
|
||||
else
|
||||
{:__block__, meta, [literal]}
|
||||
end
|
||||
end
|
||||
|
||||
# 2-tuples
|
||||
defp normalize_literal({left, right}, meta, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
if match?([_ | _], right) and keyword?(right) do
|
||||
{:__block__, meta, [{do_normalize(left, state), normalize_kw_args(right, state, true)}]}
|
||||
else
|
||||
{:__block__, meta, [{do_normalize(left, state), do_normalize(right, state)}]}
|
||||
end
|
||||
end
|
||||
|
||||
# Lists
|
||||
defp normalize_literal(list, meta, state) when is_list(list) do
|
||||
if list != [] and List.ascii_printable?(list) do
|
||||
# It's a charlist
|
||||
list =
|
||||
if state.escape do
|
||||
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), nil)
|
||||
IO.iodata_to_binary(string) |> to_charlist()
|
||||
else
|
||||
list
|
||||
end
|
||||
|
||||
meta =
|
||||
meta
|
||||
|> Keyword.put_new(:delimiter, "'")
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|
||||
{:__block__, meta, [list]}
|
||||
else
|
||||
meta =
|
||||
if line = state.parent_meta[:line] do
|
||||
meta
|
||||
|> Keyword.put_new(:closing, line: line)
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
{:__block__, meta, [normalize_kw_args(list, state, false)]}
|
||||
end
|
||||
end
|
||||
|
||||
# Probably an invalid value, wrap it and send it upstream
|
||||
defp normalize_literal(quoted, meta, _state) do
|
||||
{:__block__, meta, [quoted]}
|
||||
end
|
||||
|
||||
defp normalize_call({form, meta, args}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
arity = length(args)
|
||||
|
||||
# Only normalize the form if it's a qualified call
|
||||
form =
|
||||
if is_atom(form) do
|
||||
form
|
||||
else
|
||||
do_normalize(form, %{state | parent_meta: meta})
|
||||
end
|
||||
|
||||
meta =
|
||||
if is_nil(meta[:no_parens]) and is_nil(meta[:closing]) and is_nil(meta[:do]) and
|
||||
not Code.Formatter.local_without_parens?(form, arity, state.locals_without_parens) do
|
||||
[closing: [line: meta[:line]]] ++ meta
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
last = List.last(args)
|
||||
|
||||
cond do
|
||||
not allow_keyword?(form, arity) ->
|
||||
args = normalize_args(args, %{state | parent_meta: meta})
|
||||
{form, meta, args}
|
||||
|
||||
Keyword.has_key?(meta, :do) or match?([{{:__block__, _, [:do]}, _} | _], last) ->
|
||||
# def foo do :ok end
|
||||
# def foo, do: :ok
|
||||
normalize_kw_blocks(form, meta, args, state)
|
||||
|
||||
match?([{:do, _} | _], last) and Keyword.keyword?(last) ->
|
||||
# Non normalized kw blocks
|
||||
line = state.parent_meta[:line]
|
||||
meta = meta ++ [do: [line: line], end: [line: line]]
|
||||
normalize_kw_blocks(form, meta, args, state)
|
||||
|
||||
true ->
|
||||
args = normalize_args(args, %{state | parent_meta: meta})
|
||||
{last_arg, leading_args} = List.pop_at(args, -1, [])
|
||||
|
||||
last_args =
|
||||
case last_arg do
|
||||
{:__block__, _, [[{{:__block__, key_meta, _}, _} | _]] = last_args} ->
|
||||
if key_meta[:format] == :keyword do
|
||||
last_args
|
||||
else
|
||||
[last_arg]
|
||||
end
|
||||
|
||||
[] ->
|
||||
[]
|
||||
|
||||
_ ->
|
||||
[last_arg]
|
||||
end
|
||||
|
||||
{form, meta, leading_args ++ last_args}
|
||||
end
|
||||
end
|
||||
|
||||
defp allow_keyword?(:when, 2), do: true
|
||||
defp allow_keyword?(:{}, _), do: false
|
||||
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)
|
||||
|
||||
defp normalize_bitstring({:<<>>, meta, parts}, state, escape_interpolation \\ false) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
parts =
|
||||
if binary_interpolated?(parts) do
|
||||
normalize_interpolation_parts(parts, %{state | parent_meta: meta}, escape_interpolation)
|
||||
else
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
Enum.map(parts, fn part ->
|
||||
with {:"::", meta, [left, _]} <- part,
|
||||
true <- meta[:inferred_bitstring_spec] do
|
||||
do_normalize(left, state)
|
||||
else
|
||||
_ -> do_normalize(part, state)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
{:<<>>, meta, parts}
|
||||
end
|
||||
|
||||
defp normalize_interpolation_parts(parts, state, escape_interpolation) do
|
||||
Enum.map(parts, fn
|
||||
{:"::", interpolation_meta,
|
||||
[
|
||||
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
|
||||
{:binary, binary_meta, context}
|
||||
]} ->
|
||||
middle = do_normalize(middle, %{state | parent_meta: dot_meta})
|
||||
|
||||
{:"::", interpolation_meta,
|
||||
[
|
||||
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
|
||||
{:binary, binary_meta, context}
|
||||
]}
|
||||
|
||||
part ->
|
||||
if escape_interpolation do
|
||||
maybe_escape_literal(part, state)
|
||||
else
|
||||
part
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_map_args(args, state) do
|
||||
Enum.map(normalize_kw_args(args, state, false), fn
|
||||
{:__block__, _, [{_, _} = pair]} -> pair
|
||||
pair -> pair
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_kw_blocks(form, meta, args, state) do
|
||||
{kw_blocks, leading_args} = List.pop_at(args, -1)
|
||||
|
||||
kw_blocks =
|
||||
Enum.map(kw_blocks, fn {tag, block} ->
|
||||
block = do_normalize(block, %{state | parent_meta: meta})
|
||||
|
||||
block =
|
||||
case block do
|
||||
{_, _, [[{:->, _, _} | _] = block]} -> block
|
||||
block -> block
|
||||
end
|
||||
|
||||
# Only wrap the tag if it isn't already wrapped
|
||||
tag =
|
||||
case tag do
|
||||
{:__block__, _, _} -> tag
|
||||
_ -> {:__block__, [line: meta[:line]], [tag]}
|
||||
end
|
||||
|
||||
{tag, block}
|
||||
end)
|
||||
|
||||
leading_args = normalize_args(leading_args, %{state | parent_meta: meta})
|
||||
{form, meta, leading_args ++ [kw_blocks]}
|
||||
end
|
||||
|
||||
defp normalize_kw_args(elems, state, keyword?)
|
||||
|
||||
defp normalize_kw_args(
|
||||
[{{:__block__, key_meta, [key]}, value} = first | rest] = current,
|
||||
state,
|
||||
keyword?
|
||||
)
|
||||
when is_atom(key) do
|
||||
keyword? = keyword? or keyword?(current)
|
||||
|
||||
first =
|
||||
if key_meta[:format] == :keyword and not keyword? do
|
||||
key_meta = Keyword.delete(key_meta, :format)
|
||||
line = key_meta[:line] || meta_line(state)
|
||||
{:__block__, [line: line], [{{:__block__, key_meta, [key]}, value}]}
|
||||
else
|
||||
first
|
||||
end
|
||||
|
||||
[first | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([{left, right} | rest] = current, state, keyword?) do
|
||||
keyword? = keyword? or keyword?(current)
|
||||
|
||||
left =
|
||||
if keyword? do
|
||||
meta = [format: :keyword] ++ meta_line(state)
|
||||
{:__block__, meta, [maybe_escape_literal(left, state)]}
|
||||
else
|
||||
do_normalize(left, state)
|
||||
end
|
||||
|
||||
right = do_normalize(right, state)
|
||||
|
||||
pair =
|
||||
with {:__block__, meta, _} <- left,
|
||||
:keyword <- meta[:format] do
|
||||
{left, right}
|
||||
else
|
||||
_ -> {:__block__, meta_line(state), [{left, right}]}
|
||||
end
|
||||
|
||||
[pair | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([first | rest], state, keyword?) do
|
||||
[do_normalize(first, state) | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([], _state, _keyword?) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp normalize_args(args, state) do
|
||||
Enum.map(args, &do_normalize(&1, state))
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
|
||||
{string, _} = Code.Identifier.escape(string, nil)
|
||||
IO.iodata_to_binary(string)
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(atom, %{escape: true} = state) when is_atom(atom) do
|
||||
atom
|
||||
|> Atom.to_string()
|
||||
|> maybe_escape_literal(state)
|
||||
|> String.to_atom()
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(term, _) do
|
||||
term
|
||||
end
|
||||
|
||||
defp binary_interpolated?(parts) do
|
||||
Enum.all?(parts, fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
end
|
||||
|
||||
defp list_interpolated?(parts) do
|
||||
Enum.all?(parts, fn
|
||||
{{:., _, [Kernel, :to_string]}, _, [_]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
end
|
||||
|
||||
defp patch_meta_line(meta, parent_meta) do
|
||||
with nil <- meta[:line],
|
||||
line when is_integer(line) <- parent_meta[:line] do
|
||||
[line: line] ++ meta
|
||||
else
|
||||
_ -> meta
|
||||
end
|
||||
end
|
||||
|
||||
defp meta_line(state) do
|
||||
if line = state.parent_meta[:line] do
|
||||
[line: line]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([{{:__block__, key_meta, [key]}, _} | rest]) when is_atom(key) do
|
||||
if key_meta[:format] == :keyword do
|
||||
keyword?(rest)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_charlist(key) do
|
||||
~c"Elixir." ++ _ -> false
|
||||
_ -> keyword?(rest)
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([]), do: true
|
||||
defp keyword?(_other), do: false
|
||||
end
|
||||
@@ -1,432 +0,0 @@
|
||||
defmodule Code.Typespec do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Converts a spec clause back to Elixir quoted expression.
|
||||
"""
|
||||
@spec spec_to_quoted(atom, tuple) :: {atom, keyword, [Macro.t()]}
|
||||
def spec_to_quoted(name, spec)
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :fun, [{:type, _, :product, args}, result]})
|
||||
when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
body = {name, meta, Enum.map(args, &typespec_to_quoted/1)}
|
||||
|
||||
vars =
|
||||
for type_expr <- args ++ [result],
|
||||
var <- collect_vars(type_expr),
|
||||
uniq: true,
|
||||
do: {var, {:var, meta, nil}}
|
||||
|
||||
spec = {:"::", meta, [body, typespec_to_quoted(result)]}
|
||||
|
||||
if vars == [] do
|
||||
spec
|
||||
else
|
||||
{:when, meta, [spec, vars]}
|
||||
end
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :fun, []}) when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
{:"::", meta, [{name, meta, []}, quote(do: term)]}
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :bounded_fun, [type, constrs]}) when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
{:type, _, :fun, [{:type, _, :product, args}, result]} = type
|
||||
|
||||
guards =
|
||||
for {:type, _, :constraint, [{:atom, _, :is_subtype}, [{:var, _, var}, type]]} <- constrs do
|
||||
{erl_to_ex_var(var), typespec_to_quoted(type)}
|
||||
end
|
||||
|
||||
ignore_vars = Keyword.keys(guards)
|
||||
|
||||
vars =
|
||||
for type_expr <- args ++ [result],
|
||||
var <- collect_vars(type_expr),
|
||||
var not in ignore_vars,
|
||||
uniq: true,
|
||||
do: {var, {:var, meta, nil}}
|
||||
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
|
||||
when_args = [
|
||||
{:"::", meta, [{name, meta, args}, typespec_to_quoted(result)]},
|
||||
guards ++ vars
|
||||
]
|
||||
|
||||
{:when, meta, when_args}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a type clause back to Elixir AST.
|
||||
"""
|
||||
def type_to_quoted(type)
|
||||
|
||||
def type_to_quoted({{:record, record}, fields, args}) when is_atom(record) do
|
||||
fields = for field <- fields, do: typespec_to_quoted(field)
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
type = {:{}, [], [record | fields]}
|
||||
quote(do: unquote(record)(unquote_splicing(args)) :: unquote(type))
|
||||
end
|
||||
|
||||
def type_to_quoted({name, type, args}) when is_atom(name) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
quote(do: unquote(name)(unquote_splicing(args)) :: unquote(typespec_to_quoted(type)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all types available from the module's BEAM code.
|
||||
|
||||
The result is returned as a list of tuples where the first
|
||||
element is the type (`:typep`, `:type` and `:opaque`).
|
||||
|
||||
The module must have a corresponding BEAM file which can be
|
||||
located by the runtime system. The types will be in the Erlang
|
||||
Abstract Format.
|
||||
"""
|
||||
@spec fetch_types(module | binary) :: {:ok, [tuple]} | :error
|
||||
def fetch_types(module) when is_atom(module) or is_binary(module) do
|
||||
case typespecs_abstract_code(module) do
|
||||
{:ok, abstract_code} ->
|
||||
exported_types = for {:attribute, _, :export_type, types} <- abstract_code, do: types
|
||||
exported_types = List.flatten(exported_types)
|
||||
|
||||
types =
|
||||
for {:attribute, _, kind, {name, _, args} = type} <- abstract_code,
|
||||
kind in [:opaque, :type] do
|
||||
cond do
|
||||
kind == :opaque -> {:opaque, type}
|
||||
{name, length(args)} in exported_types -> {:type, type}
|
||||
true -> {:typep, type}
|
||||
end
|
||||
end
|
||||
|
||||
{:ok, types}
|
||||
|
||||
_ ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all specs available from the module's BEAM code.
|
||||
|
||||
The result is returned as a list of tuples where the first
|
||||
element is spec name and arity and the second is the spec.
|
||||
|
||||
The module must have a corresponding BEAM file which can be
|
||||
located by the runtime system. The types will be in the Erlang
|
||||
Abstract Format.
|
||||
"""
|
||||
@spec fetch_specs(module | binary) :: {:ok, [tuple]} | :error
|
||||
def fetch_specs(module) when is_atom(module) or is_binary(module) do
|
||||
case typespecs_abstract_code(module) do
|
||||
{:ok, abstract_code} ->
|
||||
{:ok, for({:attribute, _, :spec, value} <- abstract_code, do: value)}
|
||||
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all callbacks available from the module's BEAM code.
|
||||
|
||||
The result is returned as a list of tuples where the first
|
||||
element is spec name and arity and the second is the spec.
|
||||
|
||||
The module must have a corresponding BEAM file
|
||||
which can be located by the runtime system. The types will be
|
||||
in the Erlang Abstract Format.
|
||||
"""
|
||||
@spec fetch_callbacks(module | binary) :: {:ok, [tuple]} | :error
|
||||
def fetch_callbacks(module) when is_atom(module) or is_binary(module) do
|
||||
case typespecs_abstract_code(module) do
|
||||
{:ok, abstract_code} ->
|
||||
{:ok, for({:attribute, _, :callback, value} <- abstract_code, do: value)}
|
||||
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp typespecs_abstract_code(module) do
|
||||
with {module, binary} <- get_module_and_beam(module),
|
||||
{:ok, {_, [debug_info: {:debug_info_v1, backend, data}]}} <-
|
||||
:beam_lib.chunks(binary, [:debug_info]) do
|
||||
case data do
|
||||
{:elixir_v1, %{}, specs} ->
|
||||
# Fast path to avoid translation to Erlang from Elixir.
|
||||
{:ok, specs}
|
||||
|
||||
_ ->
|
||||
case backend.debug_info(:erlang_v1, module, data, []) do
|
||||
{:ok, abstract_code} -> {:ok, abstract_code}
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp get_module_and_beam(module) when is_atom(module) do
|
||||
with {^module, beam, _filename} <- :code.get_object_code(module),
|
||||
info_pairs when is_list(info_pairs) <- :beam_lib.info(beam),
|
||||
{:ok, ^module} <- Keyword.fetch(info_pairs, :module) do
|
||||
{module, beam}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp get_module_and_beam(beam) when is_binary(beam) do
|
||||
case :beam_lib.info(beam) do
|
||||
[_ | _] = info -> {info[:module], beam}
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
## To AST conversion
|
||||
|
||||
defp collect_vars({:ann_type, _anno, args}) when is_list(args) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp collect_vars({:type, _anno, _kind, args}) when is_list(args) do
|
||||
Enum.flat_map(args, &collect_vars/1)
|
||||
end
|
||||
|
||||
defp collect_vars({:remote_type, _anno, args}) when is_list(args) do
|
||||
Enum.flat_map(args, &collect_vars/1)
|
||||
end
|
||||
|
||||
defp collect_vars({:typed_record_field, _anno, type}) do
|
||||
collect_vars(type)
|
||||
end
|
||||
|
||||
defp collect_vars({:paren_type, _anno, [type]}) do
|
||||
collect_vars(type)
|
||||
end
|
||||
|
||||
defp collect_vars({:var, _anno, var}) do
|
||||
[erl_to_ex_var(var)]
|
||||
end
|
||||
|
||||
defp collect_vars(_) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:user_type, anno, name, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, meta(anno), args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :tuple, :any}) do
|
||||
{:tuple, meta(anno), []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :tuple, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{:{}, meta(anno), args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, :list, [{:type, _, :union, unions} = arg]}) do
|
||||
case unpack_typespec_kw(unions, []) do
|
||||
{:ok, ast} -> ast
|
||||
:error -> [typespec_to_quoted(arg)]
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :list, []}) do
|
||||
{:list, meta(anno), []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, :list, [arg]}) do
|
||||
[typespec_to_quoted(arg)]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :nonempty_list, []}) do
|
||||
[{:..., meta(anno), nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :nonempty_list, [arg]}) do
|
||||
[typespec_to_quoted(arg), {:..., meta(anno), nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :map, :any}) do
|
||||
{:map, meta(anno), []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :map, fields}) do
|
||||
fields =
|
||||
Enum.map(fields, fn
|
||||
{:type, _, :map_field_assoc, :any} ->
|
||||
{{:optional, [], [{:any, [], []}]}, {:any, [], []}}
|
||||
|
||||
{:type, _, :map_field_exact, [{:atom, _, k}, v]} ->
|
||||
{k, typespec_to_quoted(v)}
|
||||
|
||||
{:type, _, :map_field_exact, [k, v]} ->
|
||||
{{:required, [], [typespec_to_quoted(k)]}, typespec_to_quoted(v)}
|
||||
|
||||
{:type, _, :map_field_assoc, [k, v]} ->
|
||||
{{:optional, [], [typespec_to_quoted(k)]}, typespec_to_quoted(v)}
|
||||
end)
|
||||
|
||||
case List.keytake(fields, :__struct__, 0) do
|
||||
{{:__struct__, struct}, fields_pruned} when is_atom(struct) and struct != nil ->
|
||||
map_pruned = {:%{}, meta(anno), fields_pruned}
|
||||
{:%, meta(anno), [struct, map_pruned]}
|
||||
|
||||
_ ->
|
||||
{:%{}, meta(anno), fields}
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :binary, [arg1, arg2]}) do
|
||||
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_quoted(arg)
|
||||
line = meta(anno)[:line]
|
||||
|
||||
case {typespec_to_quoted(arg1), typespec_to_quoted(arg2)} do
|
||||
{arg1, 0} ->
|
||||
quote(line: line, do: <<_::unquote(arg1)>>)
|
||||
|
||||
{0, arg2} ->
|
||||
quote(line: line, do: <<_::_*unquote(arg2)>>)
|
||||
|
||||
{arg1, arg2} ->
|
||||
quote(line: line, do: <<_::unquote(arg1), _::_*unquote(arg2)>>)
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :union, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, meta(anno), [arg, expr]} end)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, [{:type, _, :product, args}, result]}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
[{:->, meta(anno), [args, typespec_to_quoted(result)]}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, [args, result]}) do
|
||||
[{:->, meta(anno), [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, []}) do
|
||||
typespec_to_quoted({:type, anno, :fun, [{:type, anno, :any}, {:type, anno, :any, []}]})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :range, [left, right]}) do
|
||||
{:.., meta(anno), [typespec_to_quoted(left), typespec_to_quoted(right)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, nil, []}) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, name, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, meta(anno), args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:var, anno, var}) do
|
||||
{erl_to_ex_var(var), meta(anno), nil}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:op, anno, op, arg}) do
|
||||
{op, meta(anno), [typespec_to_quoted(arg)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:remote_type, anno, [mod, name, args]}) do
|
||||
remote_type(anno, mod, name, args)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:ann_type, anno, [var, type]}) do
|
||||
{:"::", meta(anno), [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted(
|
||||
{:typed_record_field, {:record_field, anno1, {:atom, anno2, name}}, type}
|
||||
) do
|
||||
typespec_to_quoted({:ann_type, anno1, [{:var, anno2, name}, type]})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _, :any}) do
|
||||
quote(do: ...)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:paren_type, _, [type]}) do
|
||||
typespec_to_quoted(type)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({type, _anno, atom}) when is_atom(type) do
|
||||
atom
|
||||
end
|
||||
|
||||
defp typespec_to_quoted(other), do: other
|
||||
|
||||
## Helpers
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
|
||||
typespec_to_quoted({:type, anno, :charlist, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
|
||||
typespec_to_quoted({:type, anno, :nonempty_charlist, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
|
||||
typespec_to_quoted({:type, anno, :struct, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
|
||||
typespec_to_quoted({:type, anno, :as_boolean, [arg]})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
|
||||
typespec_to_quoted({:type, anno, :keyword, args})
|
||||
end
|
||||
|
||||
defp remote_type(anno, mod, name, args) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
dot = {:., meta(anno), [typespec_to_quoted(mod), typespec_to_quoted(name)]}
|
||||
{dot, meta(anno), args}
|
||||
end
|
||||
|
||||
defp erl_to_ex_var(var) do
|
||||
case Atom.to_string(var) do
|
||||
<<"_", c::utf8, rest::binary>> ->
|
||||
String.to_atom("_#{String.downcase(<<c::utf8>>)}#{rest}")
|
||||
|
||||
<<c::utf8, rest::binary>> ->
|
||||
String.to_atom("#{String.downcase(<<c::utf8>>)}#{rest}")
|
||||
end
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw([{:type, _, :tuple, [{:atom, _, atom}, type]} | t], acc) do
|
||||
unpack_typespec_kw(t, [{atom, typespec_to_quoted(type)} | acc])
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw([], acc) do
|
||||
{:ok, Enum.reverse(acc)}
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw(_, _acc) do
|
||||
:error
|
||||
end
|
||||
|
||||
defp meta(anno) do
|
||||
case :erl_anno.location(anno) do
|
||||
{line, column} ->
|
||||
[line: line, column: column]
|
||||
|
||||
line when is_integer(line) ->
|
||||
[line: line]
|
||||
end
|
||||
end
|
||||
end
|
||||
+34
-148
@@ -13,179 +13,65 @@ defprotocol Collectable do
|
||||
The `Enumerable` protocol is useful to take values out of a collection.
|
||||
In order to support a wide range of values, the functions provided by
|
||||
the `Enumerable` protocol do not keep shape. For example, passing a
|
||||
map to `Enum.map/2` always returns a list.
|
||||
dictionary to `Enum.map/2` always returns a list.
|
||||
|
||||
This design is intentional. `Enumerable` was designed to support infinite
|
||||
collections, resources and other structures with fixed shape. For example,
|
||||
it doesn't make sense to insert values into a `Range`, as it has a
|
||||
fixed shape where only the range limits and step are stored.
|
||||
it doesn't make sense to insert values into a range, as it has a fixed
|
||||
shape where just the range limits are stored.
|
||||
|
||||
The `Collectable` module was designed to fill the gap left by the
|
||||
`Enumerable` protocol. `Collectable.into/1` can be seen as the opposite of
|
||||
`Enumerable.reduce/3`. If the functions in `Enumerable` are about taking values out,
|
||||
then `Collectable.into/1` is about collecting those values into a structure.
|
||||
|
||||
## Examples
|
||||
|
||||
To show how to manually use the `Collectable` protocol, let's play with a
|
||||
simplified implementation for `MapSet`.
|
||||
|
||||
iex> {initial_acc, collector_fun} = Collectable.into(MapSet.new())
|
||||
iex> updated_acc = Enum.reduce([1, 2, 3], initial_acc, fn elem, acc ->
|
||||
...> collector_fun.(acc, {:cont, elem})
|
||||
...> end)
|
||||
iex> collector_fun.(updated_acc, :done)
|
||||
MapSet.new([1, 2, 3])
|
||||
|
||||
To show how the protocol can be implemented, we can again look at the
|
||||
simplified implementation for `MapSet`. In this implementation "collecting" elements
|
||||
simply means inserting them in the set through `MapSet.put/2`.
|
||||
|
||||
defimpl Collectable, for: MapSet do
|
||||
def into(map_set) do
|
||||
collector_fun = fn
|
||||
map_set_acc, {:cont, elem} ->
|
||||
MapSet.put(map_set_acc, elem)
|
||||
|
||||
map_set_acc, :done ->
|
||||
map_set_acc
|
||||
|
||||
_map_set_acc, :halt ->
|
||||
:ok
|
||||
end
|
||||
|
||||
initial_acc = map_set
|
||||
|
||||
{initial_acc, collector_fun}
|
||||
end
|
||||
end
|
||||
|
||||
So now we can call `Enum.into/2`:
|
||||
|
||||
iex> Enum.into([1, 2, 3], MapSet.new())
|
||||
MapSet.new([1, 2, 3])
|
||||
|
||||
`Enumerable` protocol. `into/1` can be seen as the opposite of
|
||||
`Enumerable.reduce/3`. If `Enumerable` is about taking values out,
|
||||
`Collectable.into/1` is about collecting those values into a structure.
|
||||
"""
|
||||
|
||||
@type command :: {:cont, term} | :done | :halt
|
||||
|
||||
@doc """
|
||||
Returns an initial accumulator and a "collector" function.
|
||||
Returns a function that collects values alongside
|
||||
the initial accumulation value.
|
||||
|
||||
Receives a `collectable` which can be used as the initial accumulator that will
|
||||
be passed to the function.
|
||||
The returned function receives a collectable and injects a given
|
||||
value into it for every `{:cont, term}` instruction.
|
||||
|
||||
The collector function receives a term and a command and injects the term into
|
||||
the collectable accumulator on every `{:cont, term}` command.
|
||||
`:done` is passed when no further values will be injected, useful
|
||||
for closing resources and normalizing values. A collectable must
|
||||
be returned on `:done`.
|
||||
|
||||
`:done` is passed as a command when no further values will be injected. This
|
||||
is useful when there's a need to close resources or normalizing values. A
|
||||
collectable must be returned when the command is `:done`.
|
||||
|
||||
If injection is suddenly interrupted, `:halt` is passed and the function
|
||||
can return any value as it won't be used.
|
||||
|
||||
For examples on how to use the `Collectable` protocol and `into/1` see the
|
||||
module documentation.
|
||||
If injection is suddenly interrupted, `:halt` is passed and it can
|
||||
return any value, as it won't be used.
|
||||
"""
|
||||
@spec into(t) :: {initial_acc :: term, collector :: (term, command -> t | term)}
|
||||
@spec into(t) :: {term, (term, command -> t | term)}
|
||||
def into(collectable)
|
||||
end
|
||||
|
||||
defimpl Collectable, for: List do
|
||||
def into(list) do
|
||||
# TODO: Change the behavior so the into always comes last on Elixir v2.0
|
||||
if list != [] do
|
||||
IO.warn(
|
||||
"the Collectable protocol is deprecated for non-empty lists. The behavior of " <>
|
||||
"Enum.into/2 and \"for\" comprehensions with an :into option is incorrect " <>
|
||||
"when collecting into non-empty lists. If you're collecting into a non-empty keyword " <>
|
||||
"list, consider using Keyword.merge/2 instead. If you're collecting into a non-empty " <>
|
||||
"list, consider concatenating the two lists with the ++ operator."
|
||||
)
|
||||
end
|
||||
|
||||
fun = fn
|
||||
list_acc, {:cont, elem} ->
|
||||
[elem | list_acc]
|
||||
|
||||
list_acc, :done ->
|
||||
list ++ :lists.reverse(list_acc)
|
||||
|
||||
_list_acc, :halt ->
|
||||
:ok
|
||||
end
|
||||
|
||||
{[], fun}
|
||||
def into(original) do
|
||||
{[], fn
|
||||
list, {:cont, x} -> [x|list]
|
||||
list, :done -> original ++ :lists.reverse(list)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: BitString do
|
||||
def into(binary) when is_binary(binary) do
|
||||
fun = fn
|
||||
acc, {:cont, x} when is_binary(x) and is_list(acc) ->
|
||||
[acc | x]
|
||||
|
||||
acc, {:cont, x} when is_bitstring(x) and is_bitstring(acc) ->
|
||||
<<acc::bitstring, x::bitstring>>
|
||||
|
||||
acc, {:cont, x} when is_bitstring(x) ->
|
||||
<<IO.iodata_to_binary(acc)::bitstring, x::bitstring>>
|
||||
|
||||
acc, :done when is_bitstring(acc) ->
|
||||
acc
|
||||
|
||||
acc, :done ->
|
||||
IO.iodata_to_binary(acc)
|
||||
|
||||
__acc, :halt ->
|
||||
:ok
|
||||
|
||||
_acc, {:cont, other} ->
|
||||
raise ArgumentError,
|
||||
"collecting into a binary requires a bitstring, got: #{inspect(other)}"
|
||||
end
|
||||
|
||||
{[binary], fun}
|
||||
end
|
||||
|
||||
def into(bitstring) do
|
||||
fun = fn
|
||||
acc, {:cont, x} when is_bitstring(x) ->
|
||||
<<acc::bitstring, x::bitstring>>
|
||||
|
||||
acc, :done ->
|
||||
acc
|
||||
|
||||
_acc, :halt ->
|
||||
:ok
|
||||
|
||||
_acc, {:cont, other} ->
|
||||
raise ArgumentError,
|
||||
"collecting into a bitstring requires a bitstring, got: #{inspect(other)}"
|
||||
end
|
||||
|
||||
{bitstring, fun}
|
||||
def into(original) do
|
||||
{original, fn
|
||||
acc, {:cont, x} when is_bitstring(x) -> [acc|x]
|
||||
acc, :done -> IO.iodata_to_binary(acc)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: Map do
|
||||
def into(map) do
|
||||
fun = fn
|
||||
map_acc, {:cont, {key, value}} ->
|
||||
Map.put(map_acc, key, value)
|
||||
|
||||
map_acc, :done ->
|
||||
map_acc
|
||||
|
||||
_map_acc, :halt ->
|
||||
:ok
|
||||
|
||||
_map_acc, {:cont, other} ->
|
||||
raise ArgumentError,
|
||||
"collecting into a map requires {key, value} tuples, got: #{inspect(other)}"
|
||||
end
|
||||
|
||||
{map, fun}
|
||||
def into(original) do
|
||||
{original, fn
|
||||
map, {:cont, {k, v}} -> :maps.put(k, v, map)
|
||||
map, :done -> map
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,361 +0,0 @@
|
||||
defmodule Config do
|
||||
@moduledoc ~S"""
|
||||
A simple keyword-based configuration API.
|
||||
|
||||
## Example
|
||||
|
||||
This module is most commonly used to define application configuration,
|
||||
typically in `config/config.exs`:
|
||||
|
||||
import Config
|
||||
|
||||
config :some_app,
|
||||
key1: "value1",
|
||||
key2: "value2"
|
||||
|
||||
import_config "#{config_env()}.exs"
|
||||
|
||||
`import Config` will import the functions `config/2`, `config/3`
|
||||
`config_env/0`, `config_target/0`, and `import_config/1`
|
||||
to help you manage your configuration.
|
||||
|
||||
`config/2` and `config/3` are used to define key-value configuration
|
||||
for a given application. Once Mix starts, it will automatically
|
||||
evaluate the configuration file and persist the configuration above
|
||||
into `:some_app`'s application environment, which can be accessed in
|
||||
as follows:
|
||||
|
||||
"value1" = Application.fetch_env!(:some_app, :key1)
|
||||
|
||||
Finally, the line `import_config "#{config_env()}.exs"` will import
|
||||
other config files based on the current configuration environment,
|
||||
such as `config/dev.exs` and `config/test.exs`.
|
||||
|
||||
`Config` also provides a low-level API for evaluating and reading
|
||||
configuration, under the `Config.Reader` module.
|
||||
|
||||
> #### Avoid application environment in libraries {: .info}
|
||||
>
|
||||
> If you are writing a library to be used by other developers,
|
||||
> it is generally recommended to avoid the application environment, as the
|
||||
> application environment is effectively a global storage. Also note that
|
||||
> the `config/config.exs` of a library is not evaluated when the library is
|
||||
> used as a dependency, as configuration is always meant to configure the
|
||||
> current project. For more information, read our [library guidelines](library-guidelines.md).
|
||||
|
||||
## Migrating from `use Mix.Config`
|
||||
|
||||
The `Config` module in Elixir was introduced in v1.9 as a replacement to
|
||||
`use Mix.Config`, which was specific to Mix and has been deprecated.
|
||||
|
||||
You can leverage `Config` instead of `use Mix.Config` in three steps. The first
|
||||
step is to replace `use Mix.Config` at the top of your config files by
|
||||
`import Config`.
|
||||
|
||||
The second is to make sure your `import_config/1` calls do not have a
|
||||
wildcard character. If so, you need to perform the wildcard lookup
|
||||
manually. For example, if you did:
|
||||
|
||||
import_config "../apps/*/config/config.exs"
|
||||
|
||||
It has to be replaced by:
|
||||
|
||||
for config <- "../apps/*/config/config.exs" |> Path.expand(__DIR__) |> Path.wildcard() do
|
||||
import_config config
|
||||
end
|
||||
|
||||
The last step is to replace all `Mix.env()` calls in the config files with `config_env()`.
|
||||
|
||||
Keep in mind you must also avoid using `Mix.env()` inside your project files.
|
||||
To check the environment at _runtime_, you may add a configuration key:
|
||||
|
||||
# config.exs
|
||||
...
|
||||
config :my_app, env: config_env()
|
||||
|
||||
Then, in other scripts and modules, you may get the environment with
|
||||
`Application.fetch_env!/2`:
|
||||
|
||||
# router.exs
|
||||
...
|
||||
if Application.fetch_env!(:my_app, :env) == :prod do
|
||||
...
|
||||
end
|
||||
|
||||
The only places where you may access functions from the `Mix` module are
|
||||
the `mix.exs` file and inside custom Mix tasks, which are always within
|
||||
the `Mix.Tasks` namespace.
|
||||
|
||||
## `config/runtime.exs`
|
||||
|
||||
For runtime configuration, you can use the `config/runtime.exs` file.
|
||||
It is executed right before applications start in both Mix and releases
|
||||
(assembled with `mix release`).
|
||||
"""
|
||||
|
||||
@opts_key {__MODULE__, :opts}
|
||||
@config_key {__MODULE__, :config}
|
||||
@imports_key {__MODULE__, :imports}
|
||||
|
||||
defp get_opts!(), do: Process.get(@opts_key) || raise_improper_use!()
|
||||
defp put_opts(value), do: Process.put(@opts_key, value)
|
||||
defp delete_opts(), do: Process.delete(@opts_key)
|
||||
|
||||
defp get_config!(), do: Process.get(@config_key) || raise_improper_use!()
|
||||
defp put_config(value), do: Process.put(@config_key, value)
|
||||
defp delete_config(), do: Process.delete(@config_key)
|
||||
|
||||
defp get_imports!(), do: Process.get(@imports_key) || raise_improper_use!()
|
||||
defp put_imports(value), do: Process.put(@imports_key, value)
|
||||
defp delete_imports(), do: Process.delete(@imports_key)
|
||||
|
||||
defp raise_improper_use!() do
|
||||
raise "could not set configuration via Config. " <>
|
||||
"This usually means you are trying to execute a configuration file " <>
|
||||
"directly, instead of reading it with Config.Reader"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Configures the given `root_key`.
|
||||
|
||||
Keyword lists are always deep-merged.
|
||||
|
||||
## Examples
|
||||
|
||||
The given `opts` are merged into the existing configuration
|
||||
for the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`, unless they are keywords, which are
|
||||
deep merged recursively. For example, the application configuration
|
||||
below
|
||||
|
||||
config :logger,
|
||||
level: :warn,
|
||||
backends: [:console]
|
||||
|
||||
config :logger,
|
||||
level: :info,
|
||||
truncate: 1024
|
||||
|
||||
will have a final configuration for `:logger` of:
|
||||
|
||||
[level: :info, backends: [:console], truncate: 1024]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
def config(root_key, opts) when is_atom(root_key) and is_list(opts) do
|
||||
unless Keyword.keyword?(opts) do
|
||||
raise ArgumentError, "config/2 expected a keyword list, got: #{inspect(opts)}"
|
||||
end
|
||||
|
||||
get_config!()
|
||||
|> __merge__([{root_key, opts}])
|
||||
|> put_config()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Configures the given `key` for the given `root_key`.
|
||||
|
||||
Keyword lists are always deep merged.
|
||||
|
||||
## Examples
|
||||
|
||||
The given `opts` are merged into the existing values for `key`
|
||||
in the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`, unless they are keywords, which are
|
||||
deep merged recursively. For example, the application configuration
|
||||
below
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :warn,
|
||||
adapter: Ecto.Adapters.Postgres,
|
||||
metadata: [read_only: true]
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :info,
|
||||
pool_size: 10,
|
||||
metadata: [replica: true]
|
||||
|
||||
will have a final value of the configuration for the `Repo`
|
||||
key in the `:ecto` application of:
|
||||
|
||||
Application.get_env(:ecto, Repo)
|
||||
#=> [
|
||||
#=> log_level: :info,
|
||||
#=> pool_size: 10,
|
||||
#=> adapter: Ecto.Adapters.Postgres,
|
||||
#=> metadata: [read_only: true, replica: true]
|
||||
#=> ]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
def config(root_key, key, opts) when is_atom(root_key) and is_atom(key) do
|
||||
get_config!()
|
||||
|> __merge__([{root_key, [{key, opts}]}])
|
||||
|> put_config()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the environment this configuration file is executed on.
|
||||
|
||||
In Mix projects this function returns the environment this configuration
|
||||
file is executed on. In releases, the environment when `mix release` ran.
|
||||
|
||||
This is most often used to execute conditional code:
|
||||
|
||||
if config_env() == :prod do
|
||||
config :my_app, :debug, false
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
defmacro config_env() do
|
||||
quote do
|
||||
Config.__env__!()
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __env__!() :: atom()
|
||||
def __env__!() do
|
||||
elem(get_opts!(), 0) || raise "no :env key was given to this configuration file"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the target this configuration file is executed on.
|
||||
|
||||
This is most often used to execute conditional code:
|
||||
|
||||
if config_target() == :host do
|
||||
config :my_app, :debug, false
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
defmacro config_target() do
|
||||
quote do
|
||||
Config.__target__!()
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __target__!() :: atom()
|
||||
def __target__!() do
|
||||
elem(get_opts!(), 1) || raise "no :target key was given to this configuration file"
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Imports configuration from the given file.
|
||||
|
||||
In case the file doesn't exist, an error is raised.
|
||||
|
||||
If file is a relative, it will be expanded relatively to the
|
||||
directory the current configuration file is in.
|
||||
|
||||
## Examples
|
||||
|
||||
This is often used to emulate configuration across environments:
|
||||
|
||||
import_config "#{config_env()}.exs"
|
||||
|
||||
Note, however, some configuration files, such as `config/runtime.exs`
|
||||
does not support imports, as they are meant to be copied across
|
||||
systems.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
defmacro import_config(file) do
|
||||
quote do
|
||||
Config.__import__!(Path.expand(unquote(file), __DIR__))
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __import__!(Path.t()) :: {term, Code.binding()}
|
||||
def __import__!(file) when is_binary(file) do
|
||||
import_config!(file, File.read!(file), true)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __eval__!(Path.t(), binary(), keyword) :: {keyword, [Path.t()] | :disabled}
|
||||
def __eval__!(file, content, opts \\ []) when is_binary(file) and is_list(opts) do
|
||||
env = Keyword.get(opts, :env)
|
||||
target = Keyword.get(opts, :target)
|
||||
imports = Keyword.get(opts, :imports, [])
|
||||
|
||||
previous_opts = put_opts({env, target})
|
||||
previous_config = put_config([])
|
||||
previous_imports = put_imports(imports)
|
||||
|
||||
try do
|
||||
{eval_config, _} = import_config!(file, content, false)
|
||||
|
||||
case get_config!() do
|
||||
[] when is_list(eval_config) ->
|
||||
{validate!(eval_config, file), get_imports!()}
|
||||
|
||||
pdict_config ->
|
||||
{pdict_config, get_imports!()}
|
||||
end
|
||||
after
|
||||
if previous_opts, do: put_opts(previous_opts), else: delete_opts()
|
||||
if previous_config, do: put_config(previous_config), else: delete_config()
|
||||
if previous_imports, do: put_imports(previous_imports), else: delete_imports()
|
||||
end
|
||||
end
|
||||
|
||||
defp import_config!(file, contents, raise_when_disabled?) do
|
||||
current_imports = get_imports!()
|
||||
|
||||
cond do
|
||||
current_imports == :disabled ->
|
||||
if raise_when_disabled? do
|
||||
raise "import_config/1 is not enabled for this configuration file. " <>
|
||||
"Some configuration files do not allow importing other files " <>
|
||||
"as they are often copied to external systems"
|
||||
end
|
||||
|
||||
file in current_imports ->
|
||||
raise ArgumentError,
|
||||
"attempting to load configuration #{Path.relative_to_cwd(file)} recursively"
|
||||
|
||||
true ->
|
||||
put_imports([file | current_imports])
|
||||
:ok
|
||||
end
|
||||
|
||||
Code.eval_string(contents, [], file: file)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __merge__(config1, config2) when is_list(config1) and is_list(config2) do
|
||||
Keyword.merge(config1, config2, fn _, app1, app2 ->
|
||||
Keyword.merge(app1, app2, &deep_merge/3)
|
||||
end)
|
||||
end
|
||||
|
||||
defp deep_merge(_key, value1, value2) do
|
||||
if Keyword.keyword?(value1) and Keyword.keyword?(value2) do
|
||||
Keyword.merge(value1, value2, &deep_merge/3)
|
||||
else
|
||||
value2
|
||||
end
|
||||
end
|
||||
|
||||
defp validate!(config, file) do
|
||||
Enum.all?(config, fn
|
||||
{app, value} when is_atom(app) ->
|
||||
if Keyword.keyword?(value) do
|
||||
true
|
||||
else
|
||||
raise ArgumentError,
|
||||
"expected config for app #{inspect(app)} in #{Path.relative_to_cwd(file)} " <>
|
||||
"to return keyword list, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
_ ->
|
||||
false
|
||||
end)
|
||||
|
||||
config
|
||||
end
|
||||
end
|
||||
@@ -1,434 +0,0 @@
|
||||
defmodule Config.Provider do
|
||||
@moduledoc """
|
||||
Specifies a provider API that loads configuration during boot.
|
||||
|
||||
Config providers are typically used during releases to load
|
||||
external configuration while the system boots. This is done
|
||||
by starting the VM with the minimum amount of applications
|
||||
running, then invoking all of the providers, and then
|
||||
restarting the system. This requires a mutable configuration
|
||||
file on disk, as the results of the providers are written to
|
||||
the file system. For more information on runtime configuration,
|
||||
see `mix release`.
|
||||
|
||||
## Multiple config files
|
||||
|
||||
One common use of config providers is to specify multiple
|
||||
configuration files in a release. Elixir ships with one provider,
|
||||
called `Config.Reader`, which is capable of handling Elixir's
|
||||
built-in config files.
|
||||
|
||||
For example, imagine you want to list some basic configuration
|
||||
on Mix's built-in `config/runtime.exs` file, but you also want
|
||||
to support additional configuration files. To do so, you can add
|
||||
this inside the `def project` portion of your `mix.exs`:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{Config.Reader, {:system, "RELEASE_ROOT", "/extra_config.exs"}}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
You can place this `extra_config.exs` file in your release in
|
||||
multiple ways:
|
||||
|
||||
1. If it is available on the host when assembling the release,
|
||||
you can place it on "rel/overlays/extra_config.exs" and it
|
||||
will be automatically copied to the release root
|
||||
|
||||
2. If it is available on the target during deployment, you can
|
||||
simply copy it to the release root as a step in your deployment
|
||||
|
||||
Now once the system boots, it will load both `config/runtime.exs`
|
||||
and `extra_config.exs` early in the boot process. You can learn
|
||||
more options on `Config.Reader`.
|
||||
|
||||
## Custom config provider
|
||||
|
||||
You can also implement custom config providers, similar to how
|
||||
`Config.Reader` works. For example, imagine you need to load
|
||||
some configuration from a JSON file and load that into the system.
|
||||
Said configuration provider would look like:
|
||||
|
||||
defmodule JSONConfigProvider do
|
||||
@behaviour Config.Provider
|
||||
|
||||
# Let's pass the path to the JSON file as config
|
||||
@impl true
|
||||
def init(path) when is_binary(path), do: path
|
||||
|
||||
@impl true
|
||||
def load(config, path) do
|
||||
# We need to start any app we may depend on.
|
||||
{:ok, _} = Application.ensure_all_started(:jason)
|
||||
|
||||
json = path |> File.read!() |> Jason.decode!()
|
||||
|
||||
Config.Reader.merge(
|
||||
config,
|
||||
my_app: [
|
||||
some_value: json["my_app_some_value"],
|
||||
another_value: json["my_app_another_value"],
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Then, when specifying your release, you can specify the provider in
|
||||
the release configuration:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{JSONConfigProvider, "/etc/config.json"}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
"""
|
||||
|
||||
@type config :: keyword
|
||||
@type state :: term
|
||||
|
||||
@typedoc """
|
||||
A path pointing to a configuration file.
|
||||
|
||||
Since configuration files are often accessed on target machines,
|
||||
it can be expressed either as:
|
||||
|
||||
* a binary representing an absolute path
|
||||
|
||||
* a `{:system, system_var, path}` tuple where the config is the
|
||||
concatenation of the environment variable `system_var` with
|
||||
the given `path`
|
||||
|
||||
"""
|
||||
@type config_path :: {:system, binary(), binary()} | binary()
|
||||
|
||||
@doc """
|
||||
Invoked when initializing a config provider.
|
||||
|
||||
A config provider is typically initialized on the machine
|
||||
where the system is assembled and not on the target machine.
|
||||
The `c:init/1` callback is useful to verify the arguments
|
||||
given to the provider and prepare the state that will be
|
||||
given to `c:load/2`.
|
||||
|
||||
Furthermore, because the state returned by `c:init/1` can
|
||||
be written to text-based config files, it should be
|
||||
restricted only to simple data types, such as integers,
|
||||
strings, atoms, tuples, maps, and lists. Entries such as
|
||||
PIDs, references, and functions cannot be serialized.
|
||||
"""
|
||||
@callback init(term) :: state
|
||||
|
||||
@doc """
|
||||
Loads configuration (typically during system boot).
|
||||
|
||||
It receives the current `config` and the `state` returned by
|
||||
`c:init/1`. Then, you typically read the extra configuration
|
||||
from an external source and merge it into the received `config`.
|
||||
Merging should be done with `Config.Reader.merge/2`, as it
|
||||
performs deep merge. It should return the updated config.
|
||||
|
||||
Note that `c:load/2` is typically invoked very early in the
|
||||
boot process, therefore if you need to use an application
|
||||
in the provider, it is your responsibility to start it.
|
||||
"""
|
||||
@callback load(config, state) :: config
|
||||
|
||||
@doc false
|
||||
defstruct [
|
||||
:providers,
|
||||
:config_path,
|
||||
extra_config: [],
|
||||
prune_runtime_sys_config_after_boot: false,
|
||||
reboot_system_after_config: false,
|
||||
validate_compile_env: false
|
||||
]
|
||||
|
||||
@reserved_apps [:kernel, :stdlib]
|
||||
|
||||
@doc """
|
||||
Validates a `t:config_path/0`.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec validate_config_path!(config_path) :: :ok
|
||||
def validate_config_path!({:system, name, path})
|
||||
when is_binary(name) and is_binary(path),
|
||||
do: :ok
|
||||
|
||||
def validate_config_path!(path) do
|
||||
if is_binary(path) and Path.type(path) != :relative do
|
||||
:ok
|
||||
else
|
||||
raise ArgumentError, """
|
||||
expected configuration path to be:
|
||||
|
||||
* a binary representing an absolute path
|
||||
* a tuple {:system, system_var, path} where the config is the \
|
||||
concatenation of the `system_var` with the given `path`
|
||||
|
||||
Got: #{inspect(path)}
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resolves a `t:config_path/0` to an actual path.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec resolve_config_path!(config_path) :: binary
|
||||
def resolve_config_path!(path) when is_binary(path), do: path
|
||||
def resolve_config_path!({:system, name, path}), do: System.fetch_env!(name) <> path
|
||||
|
||||
# Private keys
|
||||
@init_key :config_provider_init
|
||||
@booted_key :config_provider_booted
|
||||
|
||||
# Public keys
|
||||
@reboot_mode_key :config_provider_reboot_mode
|
||||
|
||||
@doc false
|
||||
def init(providers, config_path, opts \\ []) when is_list(providers) and is_list(opts) do
|
||||
validate_config_path!(config_path)
|
||||
providers = for {provider, init} <- providers, do: {provider, provider.init(init)}
|
||||
init = struct!(%Config.Provider{config_path: config_path, providers: providers}, opts)
|
||||
[elixir: [{@init_key, init}]]
|
||||
end
|
||||
|
||||
@doc false
|
||||
def boot(reboot_fun \\ &restart_and_sleep/0) do
|
||||
# The config provider typically runs very early in the
|
||||
# release process, so we need to make sure Elixir is started
|
||||
# before we go around running Elixir code.
|
||||
{:ok, _} = :application.ensure_all_started(:elixir)
|
||||
|
||||
case Application.fetch_env(:elixir, @booted_key) do
|
||||
{:ok, {:booted, path}} ->
|
||||
path && File.rm(path)
|
||||
|
||||
with {:ok, %Config.Provider{} = provider} <- Application.fetch_env(:elixir, @init_key) do
|
||||
maybe_validate_compile_env(provider)
|
||||
end
|
||||
|
||||
:booted
|
||||
|
||||
_ ->
|
||||
case Application.fetch_env(:elixir, @init_key) do
|
||||
{:ok, %Config.Provider{} = provider} ->
|
||||
path = resolve_config_path!(provider.config_path)
|
||||
reboot_config = [elixir: [{@booted_key, booted_value(provider, path)}]]
|
||||
boot_providers(path, provider, reboot_config, reboot_fun)
|
||||
|
||||
_ ->
|
||||
:skip
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp boot_providers(path, provider, reboot_config, reboot_fun) do
|
||||
original_config = read_config!(path)
|
||||
|
||||
config =
|
||||
original_config
|
||||
|> Config.__merge__(provider.extra_config)
|
||||
|> run_providers(provider)
|
||||
|
||||
if provider.reboot_system_after_config do
|
||||
config
|
||||
|> Config.__merge__(reboot_config)
|
||||
|> write_config!(path)
|
||||
|
||||
reboot_fun.()
|
||||
else
|
||||
for app <- @reserved_apps, config[app] != original_config[app] do
|
||||
abort("""
|
||||
Cannot configure #{inspect(app)} because :reboot_system_after_config has been set \
|
||||
to false and #{inspect(app)} has already been loaded, meaning any further \
|
||||
configuration won't have an effect.
|
||||
|
||||
The configuration for #{inspect(app)} before config providers was:
|
||||
|
||||
#{inspect(original_config[app])}
|
||||
|
||||
The configuration for #{inspect(app)} after config providers was:
|
||||
|
||||
#{inspect(config[app])}
|
||||
""")
|
||||
end
|
||||
|
||||
_ = Application.put_all_env(config, persistent: true)
|
||||
maybe_validate_compile_env(provider)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_validate_compile_env(provider) do
|
||||
with [_ | _] = compile_env <- provider.validate_compile_env,
|
||||
{:error, message} <- validate_compile_env(compile_env) do
|
||||
abort(message)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def valid_compile_env?(compile_env) do
|
||||
Enum.all?(compile_env, fn {app, [key | path], compile_return} ->
|
||||
try do
|
||||
traverse_env(Application.fetch_env(app, key), path) == compile_return
|
||||
rescue
|
||||
_ -> false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def validate_compile_env(compile_env, ensure_loaded? \\ true)
|
||||
|
||||
def validate_compile_env([{app, [key | path], compile_return} | compile_env], ensure_loaded?) do
|
||||
if ensure_app_loaded?(app, ensure_loaded?) do
|
||||
try do
|
||||
traverse_env(Application.fetch_env(app, key), path)
|
||||
rescue
|
||||
e ->
|
||||
{:error,
|
||||
"""
|
||||
application #{inspect(app)} failed reading its compile environment #{path(key, path)}:
|
||||
|
||||
#{Exception.format(:error, e, __STACKTRACE__)}
|
||||
|
||||
Expected it to match the compile time value of #{return_to_text(compile_return)}.
|
||||
|
||||
#{compile_env_tips(app)}
|
||||
"""}
|
||||
else
|
||||
^compile_return ->
|
||||
validate_compile_env(compile_env, ensure_loaded?)
|
||||
|
||||
runtime_return ->
|
||||
{:error,
|
||||
"""
|
||||
the application #{inspect(app)} has a different value set #{path(key, path)} \
|
||||
during runtime compared to compile time. Since this application environment entry was \
|
||||
marked as compile time, this difference can lead to different behavior than expected:
|
||||
|
||||
* Compile time value #{return_to_text(compile_return)}
|
||||
* Runtime value #{return_to_text(runtime_return)}
|
||||
|
||||
#{compile_env_tips(app)}
|
||||
"""}
|
||||
end
|
||||
else
|
||||
validate_compile_env(compile_env, ensure_loaded?)
|
||||
end
|
||||
end
|
||||
|
||||
def validate_compile_env([], _ensure_loaded?) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp ensure_app_loaded?(app, true), do: Application.ensure_loaded(app) == :ok
|
||||
defp ensure_app_loaded?(app, false), do: Application.spec(app, :vsn) != nil
|
||||
|
||||
defp path(key, []), do: "for key #{inspect(key)}"
|
||||
defp path(key, path), do: "for path #{inspect(path)} inside key #{inspect(key)}"
|
||||
|
||||
defp compile_env_tips(app),
|
||||
do: """
|
||||
To fix this error, you might:
|
||||
|
||||
* Make the runtime value match the compile time one
|
||||
|
||||
* Recompile your project. If the misconfigured application is a dependency, \
|
||||
you may need to run "mix deps.clean #{app} --build"
|
||||
|
||||
* Alternatively, you can disable this check. If you are using releases, you can \
|
||||
set :validate_compile_env to false in your release configuration. If you are \
|
||||
using Mix to start your system, you can pass the --no-validate-compile-env flag
|
||||
"""
|
||||
|
||||
defp return_to_text({:ok, value}), do: "was set to: #{inspect(value)}"
|
||||
defp return_to_text(:error), do: "was not set"
|
||||
|
||||
defp traverse_env(return, []), do: return
|
||||
defp traverse_env(:error, _paths), do: :error
|
||||
defp traverse_env({:ok, value}, [key | keys]), do: traverse_env(Access.fetch(value, key), keys)
|
||||
|
||||
@compile {:no_warn_undefined, {:init, :restart, 1}}
|
||||
defp restart_and_sleep() do
|
||||
mode = Application.get_env(:elixir, @reboot_mode_key)
|
||||
|
||||
if mode in [:embedded, :interactive] do
|
||||
:init.restart(mode: mode)
|
||||
else
|
||||
:init.restart()
|
||||
end
|
||||
|
||||
Process.sleep(:infinity)
|
||||
end
|
||||
|
||||
defp booted_value(%{prune_runtime_sys_config_after_boot: true}, path), do: {:booted, path}
|
||||
defp booted_value(%{prune_runtime_sys_config_after_boot: false}, _path), do: {:booted, nil}
|
||||
|
||||
defp read_config!(path) do
|
||||
case :file.consult(path) do
|
||||
{:ok, [inner]} ->
|
||||
inner
|
||||
|
||||
{:error, reason} ->
|
||||
bad_path_abort(
|
||||
"Could not read runtime configuration due to reason: #{inspect(reason)}",
|
||||
path
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp run_providers(config, %{providers: providers}) do
|
||||
Enum.reduce(providers, config, fn {provider, state}, acc ->
|
||||
try do
|
||||
provider.load(acc, state)
|
||||
catch
|
||||
kind, error ->
|
||||
IO.puts(:stderr, "ERROR! Config provider #{inspect(provider)} failed with:")
|
||||
IO.puts(:stderr, Exception.format(kind, error, __STACKTRACE__))
|
||||
:erlang.raise(kind, error, __STACKTRACE__)
|
||||
else
|
||||
term when is_list(term) ->
|
||||
term
|
||||
|
||||
term ->
|
||||
abort("Expected provider #{inspect(provider)} to return a list, got: #{inspect(term)}")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp write_config!(config, path) do
|
||||
contents = :io_lib.format("%% coding: utf-8~n~tw.~n", [config])
|
||||
|
||||
case File.write(path, IO.chardata_to_string(contents)) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
bad_path_abort(
|
||||
"Could not write runtime configuration due to reason: #{inspect(reason)}",
|
||||
path
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp bad_path_abort(msg, path) do
|
||||
abort(
|
||||
msg <>
|
||||
". Please make sure #{inspect(path)} is writable and accessible " <>
|
||||
"or choose a different path"
|
||||
)
|
||||
end
|
||||
|
||||
defp abort(msg) do
|
||||
IO.puts("ERROR! " <> msg)
|
||||
:erlang.raise(:error, "aborting boot", [{Config.Provider, :boot, 2, []}])
|
||||
end
|
||||
end
|
||||
@@ -1,138 +0,0 @@
|
||||
defmodule Config.Reader do
|
||||
@moduledoc """
|
||||
API for reading config files defined with `Config`.
|
||||
|
||||
## As a provider
|
||||
|
||||
`Config.Reader` can also be used as a `Config.Provider`. A config
|
||||
provider is used during releases to customize how applications are
|
||||
configured. When used as a provider, it expects a single argument:
|
||||
the configuration path (as outlined in `t:Config.Provider.config_path/0`)
|
||||
for the file to be read and loaded during the system boot.
|
||||
|
||||
For example, if you expect the target system to have a config file
|
||||
in an absolute path, you can add this inside the `def project` portion
|
||||
of your `mix.exs`:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{Config.Reader, "/etc/config.exs"}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
Or if you want to read a custom path inside the release:
|
||||
|
||||
config_providers: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
|
||||
|
||||
You can also pass a keyword list of options to the reader,
|
||||
where the `:path` is a required key:
|
||||
|
||||
config_providers: [
|
||||
{Config.Reader,
|
||||
path: "/etc/config.exs",
|
||||
env: :prod,
|
||||
imports: :disabled}
|
||||
]
|
||||
|
||||
Remember Mix already loads `config/runtime.exs` by default.
|
||||
For more examples and scenarios, see the `Config.Provider` module.
|
||||
"""
|
||||
|
||||
@behaviour Config.Provider
|
||||
|
||||
@impl true
|
||||
def init(opts) when is_list(opts) do
|
||||
{path, opts} = Keyword.pop!(opts, :path)
|
||||
Config.Provider.validate_config_path!(path)
|
||||
{path, opts}
|
||||
end
|
||||
|
||||
def init(path) do
|
||||
init(path: path)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def load(config, {path, opts}) do
|
||||
merge(config, path |> Config.Provider.resolve_config_path!() |> read!(opts))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the configuration `contents` for the given `file`.
|
||||
|
||||
Accepts the same options as `read!/2`.
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec eval!(Path.t(), binary, keyword) :: keyword
|
||||
def eval!(file, contents, opts \\ [])
|
||||
when is_binary(file) and is_binary(contents) and is_list(opts) do
|
||||
Config.__eval__!(Path.expand(file), contents, opts) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the configuration file.
|
||||
|
||||
## Options
|
||||
|
||||
* `:imports` - a list of already imported paths or `:disabled`
|
||||
to disable imports
|
||||
|
||||
* `:env` - the environment the configuration file runs on.
|
||||
See `Config.config_env/0` for sample usage
|
||||
|
||||
* `:target` - the target the configuration file runs on.
|
||||
See `Config.config_target/0` for sample usage
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read!(Path.t(), keyword) :: keyword
|
||||
def read!(file, opts \\ []) when is_binary(file) and is_list(opts) do
|
||||
file = Path.expand(file)
|
||||
Config.__eval__!(file, File.read!(file), opts) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the given configuration file and returns the configuration
|
||||
with its imports.
|
||||
|
||||
Accepts the same options as `read!/2`. Although note the `:imports`
|
||||
option cannot be disabled in `read_imports!/2`.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read_imports!(Path.t(), keyword) :: {keyword, [Path.t()]}
|
||||
def read_imports!(file, opts \\ []) when is_binary(file) and is_list(opts) do
|
||||
if opts[:imports] == :disabled do
|
||||
raise ArgumentError, ":imports must be a list of paths"
|
||||
end
|
||||
|
||||
file = Path.expand(file)
|
||||
Config.__eval__!(file, File.read!(file), opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two configurations.
|
||||
|
||||
The configurations are merged together with the values in
|
||||
the second one having higher preference than the first in
|
||||
case of conflicts. In case both values are set to keyword
|
||||
lists, it deep merges them.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Config.Reader.merge([app: [k: :v1]], [app: [k: :v2]])
|
||||
[app: [k: :v2]]
|
||||
|
||||
iex> Config.Reader.merge([app: [k: [v1: 1, v2: 2]]], [app: [k: [v2: :a, v3: :b]]])
|
||||
[app: [k: [v1: 1, v2: :a, v3: :b]]]
|
||||
|
||||
iex> Config.Reader.merge([app1: []], [app2: []])
|
||||
[app1: [], app2: []]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec merge(keyword, keyword) :: keyword
|
||||
def merge(config1, config2) when is_list(config1) and is_list(config2) do
|
||||
Config.__merge__(config1, config2)
|
||||
end
|
||||
end
|
||||
+435
-185
@@ -1,35 +1,139 @@
|
||||
defmodule Dict do
|
||||
@moduledoc ~S"""
|
||||
Generic API for dictionaries.
|
||||
This module specifies the Dict API expected to be
|
||||
implemented by different dictionaries. It also provides
|
||||
functions that redirect to the underlying Dict, allowing
|
||||
a developer to work with different Dict implementations
|
||||
using one API.
|
||||
|
||||
If you need a general dictionary, use the `Map` module.
|
||||
If you need to manipulate keyword lists, use `Keyword`.
|
||||
To create a new dict, use the `new` functions defined
|
||||
by each dict type:
|
||||
|
||||
HashDict.new #=> creates an empty HashDict
|
||||
|
||||
In the examples below, `dict_impl` means a specific
|
||||
`Dict` implementation, for example `HashDict` or `Map`.
|
||||
|
||||
## Protocols
|
||||
|
||||
Besides implementing the functions in this module, all
|
||||
dictionaries are required to implement the `Access`
|
||||
protocol:
|
||||
|
||||
iex> dict = dict_impl.new
|
||||
iex> dict = Dict.put(dict, :hello, :world)
|
||||
iex> dict[:hello]
|
||||
:world
|
||||
|
||||
As well as the `Enumerable` and `Collectable` protocols.
|
||||
|
||||
## Match
|
||||
|
||||
Dictionaries are required to implement all operations
|
||||
using the match (`===`) operator.
|
||||
|
||||
## Default implementation
|
||||
|
||||
Default implementations for some functions in the `Dict` module
|
||||
are provided via `use Dict`.
|
||||
|
||||
For example:
|
||||
|
||||
defmodule MyDict do
|
||||
use Dict
|
||||
|
||||
# implement required functions (see below)
|
||||
# override default implementations if optimization
|
||||
# is needed
|
||||
end
|
||||
|
||||
The client module must contain the following functions:
|
||||
|
||||
* `delete/2`
|
||||
* `fetch/2`
|
||||
* `put/3`
|
||||
* `reduce/3`
|
||||
* `size/1`
|
||||
|
||||
All functions, except `reduce/3`, are required by the Dict behaviour.
|
||||
`reduce/3` must be implemtented as per the Enumerable protocol.
|
||||
|
||||
Based on these functions, `Dict` generates default implementations
|
||||
for the following functions:
|
||||
|
||||
* `drop/2`
|
||||
* `equal?/2`
|
||||
* `fetch!/2`
|
||||
* `get/2`
|
||||
* `get/3`
|
||||
* `has_key?/2`
|
||||
* `keys/1`
|
||||
* `merge/2`
|
||||
* `merge/3`
|
||||
* `pop/2`
|
||||
* `pop/3`
|
||||
* `put_new/3`
|
||||
* `split/2`
|
||||
* `take/2`
|
||||
* `to_list/1`
|
||||
* `update/4`
|
||||
* `update!/3`
|
||||
* `values/1`
|
||||
|
||||
All of these functions are defined as overridable, so you can provide
|
||||
your own implementation if needed.
|
||||
|
||||
Note you can also test your custom module via `Dict`'s doctests:
|
||||
|
||||
defmodule MyDict do
|
||||
# ...
|
||||
end
|
||||
|
||||
defmodule MyTests do
|
||||
use ExUnit.Case
|
||||
doctest Dict
|
||||
defp dict_impl, do: MyDict
|
||||
end
|
||||
|
||||
To convert maps into keywords and vice-versa, use the
|
||||
`new` function in the respective modules.
|
||||
"""
|
||||
|
||||
@moduledoc deprecated: "Use Map or Keyword modules instead"
|
||||
use Behaviour
|
||||
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
@type t :: list | map
|
||||
|
||||
message =
|
||||
"Use the Map module for working with maps or the Keyword module for working with keyword lists"
|
||||
defcallback new :: t
|
||||
defcallback delete(t, key) :: t
|
||||
defcallback drop(t, Enum.t) :: t
|
||||
defcallback equal?(t, t) :: boolean
|
||||
defcallback get(t, key) :: value
|
||||
defcallback get(t, key, value) :: value
|
||||
defcallback fetch(t, key) :: {:ok, value} | :error
|
||||
defcallback fetch!(t, key) :: value | no_return
|
||||
defcallback has_key?(t, key) :: boolean
|
||||
defcallback keys(t) :: [key]
|
||||
defcallback merge(t, t) :: t
|
||||
defcallback merge(t, t, (key, value, value -> value)) :: t
|
||||
defcallback pop(t, key) :: {value, t}
|
||||
defcallback pop(t, key, value) :: {value, t}
|
||||
defcallback put(t, key, value) :: t
|
||||
defcallback put_new(t, key, value) :: t
|
||||
defcallback size(t) :: non_neg_integer()
|
||||
defcallback split(t, Enum.t) :: {t, t}
|
||||
defcallback take(t, Enum.t) :: t
|
||||
defcallback to_list(t) :: list()
|
||||
defcallback update(t, key, value, (value -> value)) :: t
|
||||
defcallback update!(t, key, (value -> value)) :: t | no_return
|
||||
defcallback values(t) :: list(value)
|
||||
|
||||
defmacro __using__(_) do
|
||||
# Use this import to guarantee proper code expansion
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
if __CALLER__.module != HashDict do
|
||||
IO.warn("use Dict is deprecated. " <> unquote(message), __CALLER__)
|
||||
end
|
||||
|
||||
quote do
|
||||
message = "Use maps and the Map module instead"
|
||||
@behaviour Dict
|
||||
|
||||
@deprecated message
|
||||
def get(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
@@ -37,22 +141,6 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def get_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> fun.()
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def get_and_update(dict, key, fun) do
|
||||
current_value = get(dict, key)
|
||||
{get, new_value} = fun.(current_value)
|
||||
{get, put(dict, key, new_value)}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def fetch!(dict, key) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
@@ -60,35 +148,23 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def has_key?(dict, key) do
|
||||
match?({:ok, _}, fetch(dict, key))
|
||||
match? {:ok, _}, fetch(dict, key)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def put_new(dict, key, value) do
|
||||
case has_key?(dict, key) do
|
||||
true -> dict
|
||||
true -> dict
|
||||
false -> put(dict, key, value)
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def put_new_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case has_key?(dict, key) do
|
||||
true -> dict
|
||||
false -> put(dict, key, fun.())
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def drop(dict, keys) do
|
||||
Enum.reduce(keys, dict, &delete(&2, &1))
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def take(dict, keys) do
|
||||
Enum.reduce(keys, new(), fn key, acc ->
|
||||
Enum.reduce(keys, new, fn key, acc ->
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> put(acc, key, value)
|
||||
:error -> acc
|
||||
@@ -96,49 +172,41 @@ defmodule Dict do
|
||||
end)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def to_list(dict) do
|
||||
reduce(dict, {:cont, []}, fn kv, acc -> {:cont, [kv | acc]} end)
|
||||
|> elem(1)
|
||||
|> :lists.reverse()
|
||||
reduce(dict, {:cont, []}, fn
|
||||
kv, acc -> {:cont, [kv|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def keys(dict) do
|
||||
reduce(dict, {:cont, []}, fn {k, _}, acc -> {:cont, [k | acc]} end)
|
||||
|> elem(1)
|
||||
|> :lists.reverse()
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{k, _}, acc -> {:cont, [k|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def values(dict) do
|
||||
reduce(dict, {:cont, []}, fn {_, v}, acc -> {:cont, [v | acc]} end)
|
||||
|> elem(1)
|
||||
|> :lists.reverse()
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{_, v}, acc -> {:cont, [v|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def equal?(dict1, dict2) do
|
||||
# Use this import to avoid conflicts in the user code
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
case size(dict1) == size(dict2) do
|
||||
false ->
|
||||
false
|
||||
|
||||
true ->
|
||||
reduce(dict1, {:cont, true}, fn {k, v}, _acc ->
|
||||
false -> false
|
||||
true ->
|
||||
reduce(dict1, {:cont, true}, fn({k, v}, _acc) ->
|
||||
case fetch(dict2, k) do
|
||||
{:ok, ^v} -> {:cont, true}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end)
|
||||
|> elem(1)
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def merge(dict1, dict2, fun \\ fn _k, _v1, v2 -> v2 end) do
|
||||
def merge(dict1, dict2, fun \\ fn(_k, _v1, v2) -> v2 end) do
|
||||
# Use this import to avoid conflicts in the user code
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
@@ -150,263 +218,444 @@ defmodule Dict do
|
||||
reduce(dict2, {:cont, dict1}, fn {k, v2}, acc ->
|
||||
{:cont, update(acc, k, v2, &fun.(k, &1, v2))}
|
||||
end)
|
||||
end
|
||||
|> elem(1)
|
||||
end |> elem(1)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update(dict, key, default, fun) do
|
||||
def update(dict, key, initial, fun) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
put(dict, key, fun.(value))
|
||||
|
||||
:error ->
|
||||
put(dict, key, default)
|
||||
put(dict, key, initial)
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update!(dict, key, fun) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
put(dict, key, fun.(value))
|
||||
|
||||
:error ->
|
||||
raise KeyError, key: key, term: dict
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def pop(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(dict, key)}
|
||||
|
||||
:error ->
|
||||
{default, dict}
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def pop_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(dict, key)}
|
||||
|
||||
:error ->
|
||||
{fun.(), dict}
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def split(dict, keys) do
|
||||
Enum.reduce(keys, {new(), dict}, fn key, {inc, exc} = acc ->
|
||||
Enum.reduce(keys, {new, dict}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
{put(inc, key, value), delete(exc, key)}
|
||||
|
||||
:error ->
|
||||
acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defoverridable merge: 2,
|
||||
merge: 3,
|
||||
equal?: 2,
|
||||
to_list: 1,
|
||||
keys: 1,
|
||||
values: 1,
|
||||
take: 2,
|
||||
drop: 2,
|
||||
get: 2,
|
||||
get: 3,
|
||||
fetch!: 2,
|
||||
has_key?: 2,
|
||||
put_new: 3,
|
||||
pop: 2,
|
||||
pop: 3,
|
||||
split: 2,
|
||||
update: 4,
|
||||
update!: 3,
|
||||
get_and_update: 3,
|
||||
get_lazy: 3,
|
||||
pop_lazy: 3,
|
||||
put_new_lazy: 3
|
||||
defoverridable merge: 2, merge: 3, equal?: 2, to_list: 1, keys: 1,
|
||||
values: 1, take: 2, drop: 2, get: 2, get: 3, fetch!: 2,
|
||||
has_key?: 2, put_new: 3, pop: 2, pop: 3, split: 2,
|
||||
update: 4, update!: 3
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
defmacrop target(dict) do
|
||||
quote do
|
||||
case unquote(dict) do
|
||||
%module{} -> module
|
||||
%{} -> Map
|
||||
dict when is_list(dict) -> Keyword
|
||||
dict -> unsupported_dict(dict)
|
||||
%{__struct__: x} when is_atom(x) ->
|
||||
x
|
||||
%{} ->
|
||||
Map
|
||||
x when is_list(x) ->
|
||||
Keyword
|
||||
x ->
|
||||
unsupported_dict(x)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns a list of all keys in `dict`.
|
||||
The keys are not guaranteed to be in any order.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> Enum.sort(Dict.keys(dict))
|
||||
[:a,:b]
|
||||
|
||||
"""
|
||||
@spec keys(t) :: [key]
|
||||
def keys(dict) do
|
||||
target(dict).keys(dict)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns a list of all values in `dict`.
|
||||
The values are not guaranteed to be in any order.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> Enum.sort(Dict.values(dict))
|
||||
[1,2]
|
||||
|
||||
"""
|
||||
@spec values(t) :: [value]
|
||||
def values(dict) do
|
||||
target(dict).values(dict)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns the number of elements in `dict`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> Dict.size(dict)
|
||||
2
|
||||
|
||||
"""
|
||||
@spec size(t) :: non_neg_integer
|
||||
def size(dict) do
|
||||
target(dict).size(dict)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns whether the given `key` exists in the given `dict`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.has_key?(dict, :a)
|
||||
true
|
||||
iex> Dict.has_key?(dict, :b)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec has_key?(t, key) :: boolean
|
||||
def has_key?(dict, key) do
|
||||
target(dict).has_key?(dict, key)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict`. If `dict` does not
|
||||
contain `key`, returns `default` (or `nil` if not provided).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.get(dict, :a)
|
||||
1
|
||||
iex> Dict.get(dict, :b)
|
||||
nil
|
||||
iex> Dict.get(dict, :b, 3)
|
||||
3
|
||||
"""
|
||||
@spec get(t, key, value) :: value
|
||||
def get(dict, key, default \\ nil) do
|
||||
target(dict).get(dict, key, default)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@spec get_lazy(t, key, (-> value)) :: value
|
||||
def get_lazy(dict, key, fun) do
|
||||
target(dict).get_lazy(dict, key, fun)
|
||||
end
|
||||
@doc """
|
||||
Returns `{:ok, value}` associated with `key` in `dict`.
|
||||
If `dict` does not contain `key`, returns `:error`.
|
||||
|
||||
@deprecated message
|
||||
@spec get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
def get_and_update(dict, key, fun) do
|
||||
target(dict).get_and_update(dict, key, fun)
|
||||
end
|
||||
## Examples
|
||||
|
||||
@deprecated message
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.fetch(dict, :a)
|
||||
{:ok, 1}
|
||||
iex> Dict.fetch(dict, :b)
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec fetch(t, key) :: value
|
||||
def fetch(dict, key) do
|
||||
target(dict).fetch(dict, key)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@spec fetch!(t, key) :: value
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict`. If `dict` does not
|
||||
contain `key`, it raises `KeyError`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.fetch!(dict, :a)
|
||||
1
|
||||
|
||||
"""
|
||||
@spec fetch!(t, key) :: value | no_return
|
||||
def fetch!(dict, key) do
|
||||
target(dict).fetch!(dict, key)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Stores the given `value` under `key` in `dict`.
|
||||
If `dict` already has `key`, the stored value is replaced by the new one.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.put(dict, :a, 3)
|
||||
iex> Dict.get(dict, :a)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec put(t, key, value) :: t
|
||||
def put(dict, key, val) do
|
||||
target(dict).put(dict, key, val)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Puts the given `value` under `key` in `dict` unless `key` already exists.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.put_new(dict, :a, 3)
|
||||
iex> Dict.get(dict, :a)
|
||||
1
|
||||
|
||||
"""
|
||||
@spec put_new(t, key, value) :: t
|
||||
def put_new(dict, key, val) do
|
||||
target(dict).put_new(dict, key, val)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@spec put_new_lazy(t, key, (-> value)) :: t
|
||||
def put_new_lazy(dict, key, fun) do
|
||||
target(dict).put_new_lazy(dict, key, fun)
|
||||
end
|
||||
@doc """
|
||||
Removes the entry stored under the given `key` from `dict`.
|
||||
If `dict` does not contain `key`, returns the dictionary unchanged.
|
||||
|
||||
@deprecated message
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.delete(dict, :a)
|
||||
iex> Dict.get(dict, :a)
|
||||
nil
|
||||
|
||||
iex> dict = Enum.into([b: 2], dict_impl.new)
|
||||
iex> Dict.delete(dict, :a) == dict
|
||||
true
|
||||
|
||||
"""
|
||||
@spec delete(t, key) :: t
|
||||
def delete(dict, key) do
|
||||
target(dict).delete(dict, key)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@spec merge(t, t) :: t
|
||||
def merge(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
@doc """
|
||||
Merges the dict `dict2` into dict `dict1`.
|
||||
|
||||
if target1 == target2 do
|
||||
target1.merge(dict1, dict2)
|
||||
else
|
||||
do_merge(target1, dict1, dict2, fn _k, _v1, v2 -> v2 end)
|
||||
end
|
||||
end
|
||||
If one of the `dict2` entries already exists in `dict1`, the
|
||||
functions in entries in `dict2` have higher precedence unless a
|
||||
function is given to resolve conflicts.
|
||||
|
||||
@deprecated message
|
||||
Notice this function is polymorphic as it merges dicts of any
|
||||
type. Each dict implementation also provides a `merge` function,
|
||||
but they can only merge dicts of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict1 = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict2 = Enum.into([a: 3, d: 4], dict_impl.new)
|
||||
iex> dict = Dict.merge(dict1, dict2)
|
||||
iex> [a: Dict.get(dict, :a), b: Dict.get(dict, :b), d: Dict.get(dict, :d)]
|
||||
[a: 3, b: 2, d: 4]
|
||||
|
||||
iex> dict1 = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict2 = Enum.into([a: 3, d: 4], dict_impl.new)
|
||||
iex> dict = Dict.merge(dict1, dict2, fn(_k, v1, v2) ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
iex> [a: Dict.get(dict, :a), b: Dict.get(dict, :b), d: Dict.get(dict, :d)]
|
||||
[a: 4, b: 2, d: 4]
|
||||
|
||||
"""
|
||||
@spec merge(t, t, (key, value, value -> value)) :: t
|
||||
def merge(dict1, dict2, fun) do
|
||||
def merge(dict1, dict2, fun \\ fn(_k, _v1, v2) -> v2 end) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
|
||||
if target1 == target2 do
|
||||
target1.merge(dict1, dict2, fun)
|
||||
else
|
||||
do_merge(target1, dict1, dict2, fun)
|
||||
Enumerable.reduce(dict2, {:cont, dict1}, fn({k, v}, acc) ->
|
||||
{:cont, target1.update(acc, k, v, fn(other) -> fun.(k, other, v) end)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_merge(target1, dict1, dict2, fun) do
|
||||
Enumerable.reduce(dict2, {:cont, dict1}, fn {k, v}, acc ->
|
||||
{:cont, target1.update(acc, k, v, fn other -> fun.(k, other, v) end)}
|
||||
end)
|
||||
|> elem(1)
|
||||
end
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict` as
|
||||
well as the `dict` without `key`.
|
||||
|
||||
@deprecated message
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {v, dict} = Dict.pop dict, :a
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{1,[]}
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {v, dict} = Dict.pop dict, :b
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{nil,[a: 1]}
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {v, dict} = Dict.pop dict, :b, 3
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{3,[a: 1]}
|
||||
|
||||
"""
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(dict, key, default \\ nil) do
|
||||
target(dict).pop(dict, key, default)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@spec pop_lazy(t, key, (-> value)) :: {value, t}
|
||||
def pop_lazy(dict, key, fun) do
|
||||
target(dict).pop_lazy(dict, key, fun)
|
||||
end
|
||||
@doc """
|
||||
Update a value in `dict` by calling `fun` on the value to get a new
|
||||
value. An exception is generated if `key` is not present in the dict.
|
||||
|
||||
@deprecated message
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.update!(dict, :a, fn(val) -> -val end)
|
||||
iex> Dict.get(dict, :a)
|
||||
-1
|
||||
|
||||
"""
|
||||
@spec update!(t, key, (value -> value)) :: t
|
||||
def update!(dict, key, fun) do
|
||||
target(dict).update!(dict, key, fun)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Update a value in `dict` by calling `fun` on the value to get a new value. If
|
||||
`key` is not present in `dict` then `initial` will be stored as the first
|
||||
value.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.update(dict, :c, 3, fn(val) -> -val end)
|
||||
iex> Dict.get(dict, :c)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(dict, key, default, fun) do
|
||||
target(dict).update(dict, key, default, fun)
|
||||
def update(dict, key, initial, fun) do
|
||||
target(dict).update(dict, key, initial, fun)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns a tuple of two dicts, where the first dict contains only
|
||||
entries from `dict` with keys in `keys`, and the second dict
|
||||
contains only entries from `dict` with keys not in `keys`
|
||||
|
||||
Any non-member keys are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2, c: 3, d: 4], dict_impl.new)
|
||||
iex> {dict1, dict2} = Dict.split(dict, [:a, :c, :e])
|
||||
iex> {Dict.to_list(dict1) |> Enum.sort, Dict.to_list(dict2) |> Enum.sort}
|
||||
{[a: 1, c: 3], [b: 2, d: 4]}
|
||||
|
||||
iex> dict = Enum.into([], dict_impl.new)
|
||||
iex> {dict1, dict2} = Dict.split(dict, [:a, :c])
|
||||
iex> {Dict.to_list(dict1), Dict.to_list(dict2)}
|
||||
{[], []}
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> {dict1, dict2} = Dict.split(dict, [:a, :b, :c])
|
||||
iex> {Dict.to_list(dict1) |> Enum.sort, Dict.to_list(dict2)}
|
||||
{[a: 1, b: 2], []}
|
||||
|
||||
"""
|
||||
@spec split(t, [key]) :: {t, t}
|
||||
def split(dict, keys) do
|
||||
target(dict).split(dict, keys)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns a new dict where the given `keys` are removed from `dict`.
|
||||
Any non-member keys are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.drop(dict, [:a, :c, :d])
|
||||
iex> Dict.to_list(dict)
|
||||
[b: 2]
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.drop(dict, [:c, :d])
|
||||
iex> Dict.to_list(dict) |> Enum.sort
|
||||
[a: 1, b: 2]
|
||||
|
||||
"""
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(dict, keys) do
|
||||
target(dict).drop(dict, keys)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns a new dict where only the keys in `keys` from `dict` are included.
|
||||
|
||||
Any non-member keys are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.take(dict, [:a, :c, :d])
|
||||
iex> Dict.to_list(dict)
|
||||
[a: 1]
|
||||
iex> dict = Dict.take(dict, [:c, :d])
|
||||
iex> Dict.to_list(dict)
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec take(t, [key]) :: t
|
||||
def take(dict, keys) do
|
||||
target(dict).take(dict, keys)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc false
|
||||
@spec empty(t) :: t
|
||||
def empty(dict) do
|
||||
target(dict).empty(dict)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Check if two dicts are equal using `===`.
|
||||
|
||||
Notice this function is polymorphic as it compares dicts of any
|
||||
type. Each dict implementation also provides an `equal?` function,
|
||||
but they can only compare dicts of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict1 = Enum.into([a: 2, b: 3, f: 5, c: 123], dict_impl.new)
|
||||
iex> dict2 = [a: 2, b: 3, f: 5, c: 123]
|
||||
iex> Dict.equal?(dict1, dict2)
|
||||
true
|
||||
|
||||
iex> dict1 = Enum.into([a: 2, b: 3, f: 5, c: 123], dict_impl.new)
|
||||
iex> dict2 = []
|
||||
iex> Dict.equal?(dict1, dict2)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
@@ -417,27 +666,28 @@ defmodule Dict do
|
||||
target1.equal?(dict1, dict2)
|
||||
|
||||
target1.size(dict1) == target2.size(dict2) ->
|
||||
Enumerable.reduce(dict2, {:cont, true}, fn {k, v}, _acc ->
|
||||
Enumerable.reduce(dict2, {:cont, true}, fn({k, v}, _acc) ->
|
||||
case target1.fetch(dict1, k) do
|
||||
{:ok, ^v} -> {:cont, true}
|
||||
_ -> {:halt, false}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end)
|
||||
|> elem(1)
|
||||
end) |> elem(1)
|
||||
|
||||
true ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
@doc """
|
||||
Returns a list of key-value pairs stored in `dict`.
|
||||
No particular order is enforced.
|
||||
"""
|
||||
@spec to_list(t) :: list
|
||||
def to_list(dict) do
|
||||
target(dict).to_list(dict)
|
||||
end
|
||||
|
||||
@spec unsupported_dict(t) :: no_return
|
||||
defp unsupported_dict(dict) do
|
||||
raise ArgumentError, "unsupported dict: #{inspect(dict)}"
|
||||
raise ArgumentError, "unsupported dict: #{inspect dict}"
|
||||
end
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+1076
-3713
File diff suppressed because it is too large
Load Diff
+250
-1968
File diff suppressed because it is too large
Load Diff
+441
-1162
File diff suppressed because it is too large
Load Diff
+15
-35
@@ -2,17 +2,17 @@ require Record
|
||||
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
A struct that holds file information.
|
||||
A struct responsible to hold file information.
|
||||
|
||||
In Erlang, this struct is represented by a `:file_info` record.
|
||||
Therefore this module also provides functions for converting
|
||||
between the Erlang record and the Elixir struct.
|
||||
in between the Erlang record and the Elixir struct.
|
||||
|
||||
Its fields are:
|
||||
|
||||
* `size` - size of file in bytes.
|
||||
|
||||
* `type` - `:device | :directory | :regular | :other | :symlink`; the type of the
|
||||
* `type` - `:device | :directory | :regular | :other`; the type of the
|
||||
file.
|
||||
|
||||
* `access` - `:read | :write | :read_write | :none`; the current system
|
||||
@@ -23,7 +23,7 @@ defmodule File.Stat do
|
||||
* `mtime` - the last time the file was written.
|
||||
|
||||
* `ctime` - the interpretation of this time field depends on the operating
|
||||
system. On Unix-like operating systems, it is the last time the file or the inode was changed.
|
||||
system. On Unix, it is the last time the file or the inode was changed.
|
||||
In Windows, it is the time of creation.
|
||||
|
||||
* `mode` - the file permissions.
|
||||
@@ -32,53 +32,36 @@ defmodule File.Stat do
|
||||
systems which have no concept of links.
|
||||
|
||||
* `major_device` - identifies the file system where the file is located.
|
||||
In Windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
In windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
B:, and so on.
|
||||
|
||||
* `minor_device` - only valid for character devices on Unix-like systems. In all other
|
||||
* `minor_device` - only valid for character devices on Unix. In all other
|
||||
cases, this field is zero.
|
||||
|
||||
* `inode` - gives the inode number. On non-Unix-like file systems, this field
|
||||
* `inode` - gives the inode number. On non-Unix file systems, this field
|
||||
will be zero.
|
||||
|
||||
* `uid` - indicates the owner of the file. Will be zero for non-Unix-like file
|
||||
systems.
|
||||
* `uid` - indicates the owner of the file.
|
||||
|
||||
* `gid` - indicates the group that owns the file. Will be zero for
|
||||
non-Unix-like file systems.
|
||||
* `gid` - gives the group that the owner of the file belongs to. Will be
|
||||
zero for non-Unix file systems.
|
||||
|
||||
The time type returned in `atime`, `mtime`, and `ctime` is dependent on the
|
||||
time type set in options. `{:time, type}` where type can be `:local`,
|
||||
`:universal`, or `:posix`. Default is `:universal`.
|
||||
`:universal`, or `:posix`. Default is `:local`.
|
||||
"""
|
||||
|
||||
record = Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
||||
keys = :lists.map(&elem(&1, 0), record)
|
||||
vals = :lists.map(&{&1, [], nil}, keys)
|
||||
pairs = :lists.zip(keys, vals)
|
||||
keys = :lists.map(&elem(&1, 0), record)
|
||||
vals = :lists.map(&{&1, [], nil}, keys)
|
||||
pairs = :lists.zip(keys, vals)
|
||||
|
||||
defstruct keys
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
size: non_neg_integer() | :undefined,
|
||||
type: :device | :directory | :regular | :other | :symlink | :undefined,
|
||||
access: :read | :write | :read_write | :none | :undefined,
|
||||
atime: :calendar.datetime() | integer() | :undefined,
|
||||
mtime: :calendar.datetime() | integer() | :undefined,
|
||||
ctime: :calendar.datetime() | integer() | :undefined,
|
||||
mode: non_neg_integer() | :undefined,
|
||||
links: non_neg_integer() | :undefined,
|
||||
major_device: non_neg_integer() | :undefined,
|
||||
minor_device: non_neg_integer() | :undefined,
|
||||
inode: non_neg_integer() | :undefined,
|
||||
uid: non_neg_integer() | :undefined,
|
||||
gid: non_neg_integer() | :undefined
|
||||
}
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc """
|
||||
Converts a `File.Stat` struct to a `:file_info` record.
|
||||
"""
|
||||
@spec to_record(t()) :: :file.file_info()
|
||||
def to_record(%File.Stat{unquote_splicing(pairs)}) do
|
||||
{:file_info, unquote_splicing(vals)}
|
||||
end
|
||||
@@ -86,9 +69,6 @@ defmodule File.Stat do
|
||||
@doc """
|
||||
Converts a `:file_info` record into a `File.Stat`.
|
||||
"""
|
||||
@spec from_record(:file.file_info()) :: t()
|
||||
def from_record(file_info)
|
||||
|
||||
def from_record({:file_info, unquote_splicing(vals)}) do
|
||||
%File.Stat{unquote_splicing(pairs)}
|
||||
end
|
||||
|
||||
+31
-175
@@ -7,61 +7,45 @@ defmodule File.Stream do
|
||||
* `path` - the file path
|
||||
* `modes` - the file modes
|
||||
* `raw` - a boolean indicating if bin functions should be used
|
||||
* `line_or_bytes` - if reading should read lines or a given number of bytes
|
||||
* `node` - the node the file belongs to
|
||||
* `line_or_bytes` - if reading should read lines or a given amount of bytes
|
||||
|
||||
"""
|
||||
|
||||
defstruct path: nil, modes: [], line_or_bytes: :line, raw: true, node: nil
|
||||
defstruct path: nil, modes: [], line_or_bytes: :line, raw: true
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc false
|
||||
def __build__(path, line_or_bytes, modes) do
|
||||
with {:read_offset, offset} <- :lists.keyfind(:read_offset, 1, modes),
|
||||
false <- is_integer(offset) and offset >= 0 do
|
||||
raise ArgumentError,
|
||||
"expected :read_offset to be a non-negative integer, got: #{inspect(offset)}"
|
||||
end
|
||||
|
||||
def __build__(path, modes, line_or_bytes) do
|
||||
raw = :lists.keyfind(:encoding, 1, modes) == false
|
||||
|
||||
modes =
|
||||
case raw do
|
||||
true ->
|
||||
case :lists.keyfind(:read_ahead, 1, modes) do
|
||||
{:read_ahead, false} -> [:raw | :lists.keydelete(:read_ahead, 1, modes)]
|
||||
{:read_ahead, _} -> [:raw | modes]
|
||||
false -> [:raw, :read_ahead | modes]
|
||||
end
|
||||
|
||||
false ->
|
||||
modes
|
||||
if raw do
|
||||
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
|
||||
[:raw|modes]
|
||||
else
|
||||
[:raw, :read_ahead|modes]
|
||||
end
|
||||
else
|
||||
modes
|
||||
end
|
||||
|
||||
%File.Stream{path: path, modes: modes, raw: raw, line_or_bytes: line_or_bytes, node: node()}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __open__(%File.Stream{path: path, node: node}, modes) when node == node() do
|
||||
:file.open(path, modes)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __open__(%File.Stream{path: path, node: node}, modes) do
|
||||
:erpc.call(node, :file_io_server, :start, [self(), path, List.delete(modes, :raw)])
|
||||
%File.Stream{path: path, modes: modes, raw: raw, line_or_bytes: line_or_bytes}
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def into(%{modes: modes, raw: raw} = stream) do
|
||||
modes = for mode <- modes, mode not in [:read], do: mode
|
||||
def empty(stream) do
|
||||
stream
|
||||
end
|
||||
|
||||
case File.Stream.__open__(stream, [:write | modes]) do
|
||||
def into(%{path: path, modes: modes, raw: raw} = stream) do
|
||||
modes = for mode <- modes, not mode in [:read], do: mode
|
||||
|
||||
case :file.open(path, [:write|modes]) do
|
||||
{:ok, device} ->
|
||||
{:ok, into(device, stream, raw)}
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: stream.path
|
||||
raise File.Error, reason: reason, action: "stream", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -69,16 +53,14 @@ defmodule File.Stream do
|
||||
fn
|
||||
:ok, {:cont, x} ->
|
||||
case raw do
|
||||
true -> IO.binwrite(device, x)
|
||||
true -> IO.binwrite(device, x)
|
||||
false -> IO.write(device, x)
|
||||
end
|
||||
|
||||
:ok, :done ->
|
||||
# If delayed_write option is used and the last write failed will
|
||||
# MatchError here as {:error, _} is returned.
|
||||
:ok = :file.close(device)
|
||||
stream
|
||||
|
||||
:ok, :halt ->
|
||||
# If delayed_write option is used and the last write failed will
|
||||
# MatchError here as {:error, _} is returned.
|
||||
@@ -88,56 +70,27 @@ defmodule File.Stream do
|
||||
end
|
||||
|
||||
defimpl Enumerable do
|
||||
@read_ahead_size 64 * 1024
|
||||
def reduce(%{path: path, modes: modes, line_or_bytes: line_or_bytes, raw: raw}, acc, fun) do
|
||||
modes = for mode <- modes, not mode in [:write, :append], do: mode
|
||||
|
||||
def reduce(%{modes: modes, line_or_bytes: line_or_bytes, raw: raw} = stream, acc, fun) do
|
||||
start_fun = fn ->
|
||||
case File.Stream.__open__(stream, read_modes(modes)) do
|
||||
{:ok, device} ->
|
||||
skip_bom_and_offset(device, raw, modes)
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: stream.path
|
||||
start_fun =
|
||||
fn ->
|
||||
case :file.open(path, modes) do
|
||||
{:ok, device} -> device
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
next_fun =
|
||||
case raw do
|
||||
true -> &IO.each_binstream(&1, line_or_bytes)
|
||||
true -> &IO.each_binstream(&1, line_or_bytes)
|
||||
false -> &IO.each_stream(&1, line_or_bytes)
|
||||
end
|
||||
|
||||
Stream.resource(start_fun, next_fun, &:file.close/1).(acc, fun)
|
||||
end
|
||||
|
||||
def count(%{modes: modes, line_or_bytes: :line, path: path, raw: raw} = stream) do
|
||||
pattern = :binary.compile_pattern("\n")
|
||||
|
||||
counter = fn device ->
|
||||
device = skip_bom_and_offset(device, raw, modes)
|
||||
count_lines(device, path, pattern, read_function(stream), 0)
|
||||
end
|
||||
|
||||
{:ok, open!(stream, modes, counter)}
|
||||
end
|
||||
|
||||
def count(%{path: path, line_or_bytes: bytes, raw: true, modes: modes, node: node} = stream) do
|
||||
case :erpc.call(node, File, :stat, [path]) do
|
||||
{:ok, %{size: 0}} ->
|
||||
{:error, __MODULE__}
|
||||
|
||||
{:ok, %{size: size}} ->
|
||||
bom_offset = count_raw_bom(stream, modes)
|
||||
offset = get_read_offset(modes)
|
||||
size = max(size - bom_offset - offset, 0)
|
||||
remainder = if rem(size, bytes) == 0, do: 0, else: 1
|
||||
{:ok, div(size, bytes) + remainder}
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: path
|
||||
end
|
||||
end
|
||||
|
||||
def count(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
@@ -145,102 +98,5 @@ defmodule File.Stream do
|
||||
def member?(_stream, _term) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
def slice(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
defp open!(stream, modes, fun) do
|
||||
case File.Stream.__open__(stream, read_modes(modes)) do
|
||||
{:ok, device} ->
|
||||
try do
|
||||
fun.(device)
|
||||
after
|
||||
:file.close(device)
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: stream.path
|
||||
end
|
||||
end
|
||||
|
||||
defp count_raw_bom(stream, modes) do
|
||||
if :trim_bom in modes do
|
||||
open!(stream, read_modes(modes), &(&1 |> trim_bom(true) |> elem(1)))
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
defp skip_bom_and_offset(device, raw, modes) do
|
||||
device =
|
||||
if :trim_bom in modes do
|
||||
device |> trim_bom(raw) |> elem(0)
|
||||
else
|
||||
device
|
||||
end
|
||||
|
||||
offset = get_read_offset(modes)
|
||||
|
||||
if offset > 0 do
|
||||
{:ok, _} = :file.position(device, {:cur, offset})
|
||||
end
|
||||
|
||||
device
|
||||
end
|
||||
|
||||
defp trim_bom(device, true) do
|
||||
bom_length = device |> IO.binread(4) |> bom_length()
|
||||
{:ok, new_pos} = :file.position(device, bom_length)
|
||||
{device, new_pos}
|
||||
end
|
||||
|
||||
defp trim_bom(device, false) do
|
||||
# Or we read the bom in the correct amount or it isn't there
|
||||
case bom_length(IO.read(device, 1)) do
|
||||
0 ->
|
||||
{:ok, _} = :file.position(device, 0)
|
||||
{device, 0}
|
||||
|
||||
_ ->
|
||||
{device, 1}
|
||||
end
|
||||
end
|
||||
|
||||
defp bom_length(<<239, 187, 191, _rest::binary>>), do: 3
|
||||
defp bom_length(<<254, 255, _rest::binary>>), do: 2
|
||||
defp bom_length(<<255, 254, _rest::binary>>), do: 2
|
||||
defp bom_length(<<0, 0, 254, 255, _rest::binary>>), do: 4
|
||||
defp bom_length(<<254, 255, 0, 0, _rest::binary>>), do: 4
|
||||
defp bom_length(_binary), do: 0
|
||||
|
||||
def get_read_offset(modes) do
|
||||
case :lists.keyfind(:read_offset, 1, modes) do
|
||||
{:read_offset, offset} -> offset
|
||||
false -> 0
|
||||
end
|
||||
end
|
||||
|
||||
defp read_modes(modes) do
|
||||
for mode <- modes, mode not in [:write, :append, :trim_bom], do: mode
|
||||
end
|
||||
|
||||
defp count_lines(device, path, pattern, read, count) do
|
||||
case read.(device) do
|
||||
data when is_binary(data) ->
|
||||
count_lines(device, path, pattern, read, count + count_lines(data, pattern))
|
||||
|
||||
:eof ->
|
||||
count
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: path
|
||||
end
|
||||
end
|
||||
|
||||
defp count_lines(data, pattern), do: length(:binary.matches(data, pattern))
|
||||
|
||||
defp read_function(%{raw: true}), do: &IO.binread(&1, @read_ahead_size)
|
||||
defp read_function(%{raw: false}), do: &IO.read(&1, @read_ahead_size)
|
||||
end
|
||||
end
|
||||
|
||||
+172
-548
@@ -2,651 +2,275 @@ import Kernel, except: [round: 1]
|
||||
|
||||
defmodule Float do
|
||||
@moduledoc """
|
||||
Functions for working with floating-point numbers.
|
||||
|
||||
For mathematical operations on top of floating-points,
|
||||
see Erlang's [`:math`](`:math`) module.
|
||||
|
||||
## Kernel functions
|
||||
|
||||
There are functions related to floating-point numbers on the `Kernel` module
|
||||
too. Here is a list of them:
|
||||
|
||||
* `Kernel.round/1`: rounds a number to the nearest integer.
|
||||
* `Kernel.trunc/1`: returns the integer part of a number.
|
||||
|
||||
## Known issues
|
||||
|
||||
There are some very well known problems with floating-point numbers
|
||||
and arithmetic due to the fact most decimal fractions cannot be
|
||||
represented by a floating-point binary and most operations are not exact,
|
||||
but operate on approximations. Those issues are not specific
|
||||
to Elixir, they are a property of floating point representation itself.
|
||||
|
||||
For example, the numbers 0.1 and 0.01 are two of them, what means the result
|
||||
of squaring 0.1 does not give 0.01 neither the closest representable. Here is
|
||||
what happens in this case:
|
||||
|
||||
* The closest representable number to 0.1 is 0.1000000014
|
||||
* The closest representable number to 0.01 is 0.0099999997
|
||||
* Doing 0.1 * 0.1 should return 0.01, but because 0.1 is actually 0.1000000014,
|
||||
the result is 0.010000000000000002, and because this is not the closest
|
||||
representable number to 0.01, you'll get the wrong result for this operation
|
||||
|
||||
There are also other known problems like flooring or rounding numbers. See
|
||||
`round/2` and `floor/2` for more details about them.
|
||||
|
||||
To learn more about floating-point arithmetic visit:
|
||||
|
||||
* [0.30000000000000004.com](http://0.30000000000000004.com/)
|
||||
* [What Every Programmer Should Know About Floating-Point Arithmetic](https://floating-point-gui.de/)
|
||||
|
||||
Functions for working with floating point numbers.
|
||||
"""
|
||||
|
||||
import Bitwise
|
||||
|
||||
@power_of_2_to_52 4_503_599_627_370_496
|
||||
@precision_range 0..15
|
||||
@type precision_range :: 0..15
|
||||
|
||||
@min_finite then(<<0xFFEFFFFFFFFFFFFF::64>>, fn <<num::float>> -> num end)
|
||||
@max_finite then(<<0x7FEFFFFFFFFFFFFF::64>>, fn <<num::float>> -> num end)
|
||||
|
||||
@doc """
|
||||
Returns the maximum finite value for a float.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.max_finite()
|
||||
1.7976931348623157e308
|
||||
|
||||
"""
|
||||
@spec max_finite() :: float
|
||||
def max_finite, do: @max_finite
|
||||
|
||||
@doc """
|
||||
Returns the minimum finite value for a float.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.min_finite()
|
||||
-1.7976931348623157e308
|
||||
|
||||
"""
|
||||
@spec min_finite() :: float
|
||||
def min_finite, do: @min_finite
|
||||
|
||||
@doc """
|
||||
Computes `base` raised to power of `exponent`.
|
||||
|
||||
`base` must be a float and `exponent` can be any number.
|
||||
However, if a negative base and a fractional exponent
|
||||
are given, it raises `ArithmeticError`.
|
||||
|
||||
It always returns a float. See `Integer.pow/2` for
|
||||
exponentiation that returns integers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.pow(2.0, 0)
|
||||
1.0
|
||||
iex> Float.pow(2.0, 1)
|
||||
2.0
|
||||
iex> Float.pow(2.0, 10)
|
||||
1024.0
|
||||
iex> Float.pow(2.0, -1)
|
||||
0.5
|
||||
iex> Float.pow(2.0, -3)
|
||||
0.125
|
||||
|
||||
iex> Float.pow(3.0, 1.5)
|
||||
5.196152422706632
|
||||
|
||||
iex> Float.pow(-2.0, 3)
|
||||
-8.0
|
||||
iex> Float.pow(-2.0, 4)
|
||||
16.0
|
||||
|
||||
iex> Float.pow(-1.0, 0.5)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec pow(float, number) :: float
|
||||
def pow(base, exponent) when is_float(base) and is_number(exponent),
|
||||
do: :math.pow(base, exponent)
|
||||
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
If successful, returns a tuple in the form of `{float, remainder_of_binary}`;
|
||||
when the binary cannot be coerced into a valid float, the atom `:error` is
|
||||
returned.
|
||||
|
||||
If the size of float exceeds the maximum size of `1.7976931348623157e+308`,
|
||||
`:error` is returned even though the textual representation itself might be
|
||||
well formed.
|
||||
|
||||
If you want to convert a string-formatted float directly to a float,
|
||||
`String.to_float/1` can be used instead.
|
||||
If successful, returns a tuple of the form `{float, remainder_of_binary}`.
|
||||
Otherwise `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.parse("34")
|
||||
{34.0, ""}
|
||||
{34.0,""}
|
||||
|
||||
iex> Float.parse("34.25")
|
||||
{34.25, ""}
|
||||
{34.25,""}
|
||||
|
||||
iex> Float.parse("56.5xyz")
|
||||
{56.5, "xyz"}
|
||||
{56.5,"xyz"}
|
||||
|
||||
iex> Float.parse("pi")
|
||||
:error
|
||||
iex> Float.parse("1.7976931348623159e+308")
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec parse(binary) :: {float, binary} | :error
|
||||
def parse("-" <> binary) do
|
||||
case parse_unsigned(binary) do
|
||||
case parse_unsign(binary) do
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
def parse("+" <> binary) do
|
||||
parse_unsigned(binary)
|
||||
end
|
||||
|
||||
def parse(binary) do
|
||||
parse_unsigned(binary)
|
||||
parse_unsign(binary)
|
||||
end
|
||||
|
||||
defp parse_unsigned(<<digit, rest::binary>>) when digit in ?0..?9,
|
||||
do: parse_unsigned(rest, false, false, <<digit>>)
|
||||
|
||||
defp parse_unsigned(binary) when is_binary(binary), do: :error
|
||||
|
||||
defp parse_unsigned(<<digit, rest::binary>>, dot?, e?, acc) when digit in ?0..?9,
|
||||
do: parse_unsigned(rest, dot?, e?, <<acc::binary, digit>>)
|
||||
|
||||
defp parse_unsigned(<<?., digit, rest::binary>>, false, false, acc) when digit in ?0..?9,
|
||||
do: parse_unsigned(rest, true, false, <<acc::binary, ?., digit>>)
|
||||
|
||||
defp parse_unsigned(<<exp_marker, digit, rest::binary>>, dot?, false, acc)
|
||||
when exp_marker in ~c"eE" and digit in ?0..?9,
|
||||
do: parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, digit>>)
|
||||
|
||||
defp parse_unsigned(<<exp_marker, sign, digit, rest::binary>>, dot?, false, acc)
|
||||
when exp_marker in ~c"eE" and sign in ~c"-+" and digit in ?0..?9,
|
||||
do: parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, sign, digit>>)
|
||||
|
||||
# When floats are expressed in scientific notation, :erlang.binary_to_float/1 can raise an
|
||||
# ArgumentError if the e exponent is too big. For example, "1.0e400". Because of this, we
|
||||
# rescue the ArgumentError here and return an error.
|
||||
defp parse_unsigned(rest, dot?, true = _e?, acc) do
|
||||
:erlang.binary_to_float(add_dot(acc, dot?))
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
else
|
||||
float -> {float, rest}
|
||||
defp parse_unsign("-" <> _), do: :error
|
||||
defp parse_unsign(binary) when is_binary(binary) do
|
||||
case Integer.parse binary do
|
||||
:error -> :error
|
||||
{integer_part, after_integer} -> parse_unsign after_integer, integer_part
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_unsigned(rest, dot?, false = _e?, acc),
|
||||
do: {:erlang.binary_to_float(add_dot(acc, dot?)), rest}
|
||||
# Dot followed by digit is required afterwards or we are done
|
||||
defp parse_unsign(<< ?., char, rest :: binary >>, int) when char in ?0..?9 do
|
||||
parse_unsign(rest, char - ?0, 1, int)
|
||||
end
|
||||
|
||||
defp add_dot(acc, true), do: acc
|
||||
defp add_dot(acc, false), do: acc <> ".0"
|
||||
defp parse_unsign(rest, int) do
|
||||
{:erlang.float(int), rest}
|
||||
end
|
||||
|
||||
# Handle decimal points
|
||||
defp parse_unsign(<< char, rest :: binary >>, float, decimal, int) when char in ?0..?9 do
|
||||
parse_unsign rest, 10 * float + (char - ?0), decimal + 1, int
|
||||
end
|
||||
|
||||
defp parse_unsign(<< ?e, after_e :: binary >>, float, decimal, int) do
|
||||
case Integer.parse after_e do
|
||||
:error ->
|
||||
# Note we rebuild the binary here instead of breaking it apart at
|
||||
# the function clause because the current approach copies a binary
|
||||
# just on this branch. If we broke it apart in the function clause,
|
||||
# the copy would happen when calling Integer.parse/1.
|
||||
{floatify(int, float, decimal), << ?e, after_e :: binary >>}
|
||||
{exponential, after_exponential} ->
|
||||
{floatify(int, float, decimal, exponential), after_exponential}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_unsign(bitstring, float, decimal, int) do
|
||||
{floatify(int, float, decimal), bitstring}
|
||||
end
|
||||
|
||||
defp floatify(int, float, decimal, exponential \\ 0) do
|
||||
multiplier = if int < 0, do: -1.0, else: 1.0
|
||||
|
||||
# Try to ensure the minimum amount of rounding errors
|
||||
result = multiplier * (abs(int) * :math.pow(10, decimal) + float) * :math.pow(10, exponential - decimal)
|
||||
|
||||
# Try avoiding stuff like this:
|
||||
# iex(1)> 0.0001 * 75
|
||||
# 0.007500000000000001
|
||||
# Due to IEEE 754 floating point standard
|
||||
# http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
|
||||
|
||||
final_decimal_places = decimal - exponential
|
||||
if final_decimal_places > 0 do
|
||||
decimal_power_round = :math.pow(10, final_decimal_places)
|
||||
trunc(result * decimal_power_round) / decimal_power_round
|
||||
else
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a float to the largest float less than or equal to `number`.
|
||||
Rounds a float to the largest integer less than or equal to `num`.
|
||||
|
||||
`floor/2` also accepts a precision to round a floating-point value down
|
||||
Floor also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
The operation is performed on the binary floating point, without a
|
||||
conversion to decimal.
|
||||
|
||||
This function always returns a float. `Kernel.trunc/1` may be used instead to
|
||||
This function always returns floats. One may use `Kernel.trunc/1` to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
## Known issues
|
||||
|
||||
The behavior of `floor/2` for floats can be surprising. For example:
|
||||
|
||||
iex> Float.floor(12.52, 2)
|
||||
12.51
|
||||
|
||||
One may have expected it to floor to 12.52. This is not a bug.
|
||||
Most decimal fractions cannot be represented as a binary floating point
|
||||
and therefore the number above is internally represented as 12.51999999,
|
||||
which explains the behavior above.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.floor(34.25)
|
||||
34.0
|
||||
|
||||
iex> Float.floor(-56.5)
|
||||
-57.0
|
||||
iex> Float.floor(34.259, 2)
|
||||
|
||||
iex> Float.floor(34.253, 2)
|
||||
34.25
|
||||
|
||||
"""
|
||||
@spec floor(float, precision_range) :: float
|
||||
def floor(number, precision \\ 0)
|
||||
|
||||
def floor(number, 0) when is_float(number) do
|
||||
:math.floor(number)
|
||||
end
|
||||
|
||||
def floor(number, precision) when is_float(number) and precision in @precision_range do
|
||||
round(number, precision, :floor)
|
||||
end
|
||||
|
||||
def floor(number, precision) when is_float(number) do
|
||||
raise ArgumentError, invalid_precision_message(precision)
|
||||
@spec floor(float, 0..15) :: float
|
||||
def floor(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
number = number * power
|
||||
truncated = trunc(number)
|
||||
variance = if number - truncated < 0, do: -1.0, else: 0.0
|
||||
(truncated + variance) / power
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a float to the smallest float greater than or equal to `number`.
|
||||
Rounds a float to the largest integer greater than or equal to `num`.
|
||||
|
||||
`ceil/2` also accepts a precision to round a floating-point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
Ceil also accepts a precision to round a floating point value down to
|
||||
an arbitrary number of fractional digits (between 0 and 15).
|
||||
|
||||
The operation is performed on the binary floating point, without a
|
||||
conversion to decimal.
|
||||
|
||||
The behavior of `ceil/2` for floats can be surprising. For example:
|
||||
|
||||
iex> Float.ceil(-12.52, 2)
|
||||
-12.51
|
||||
|
||||
One may have expected it to ceil to -12.52. This is not a bug.
|
||||
Most decimal fractions cannot be represented as a binary floating point
|
||||
and therefore the number above is internally represented as -12.51999999,
|
||||
which explains the behavior above.
|
||||
|
||||
This function always returns floats. `Kernel.trunc/1` may be used instead to
|
||||
This function always returns floats. One may use `Kernel.trunc/1` to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.ceil(34.25)
|
||||
35.0
|
||||
|
||||
iex> Float.ceil(-56.5)
|
||||
-56.0
|
||||
iex> Float.ceil(34.251, 2)
|
||||
|
||||
iex> Float.ceil(34.253, 2)
|
||||
34.26
|
||||
iex> Float.ceil(-0.01)
|
||||
-0.0
|
||||
|
||||
"""
|
||||
@spec ceil(float, precision_range) :: float
|
||||
def ceil(number, precision \\ 0)
|
||||
|
||||
def ceil(number, 0) when is_float(number) do
|
||||
:math.ceil(number)
|
||||
end
|
||||
|
||||
def ceil(number, precision) when is_float(number) and precision in @precision_range do
|
||||
round(number, precision, :ceil)
|
||||
end
|
||||
|
||||
def ceil(number, precision) when is_float(number) do
|
||||
raise ArgumentError, invalid_precision_message(precision)
|
||||
@spec ceil(float, 0..15) :: float
|
||||
def ceil(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
number = number * power
|
||||
truncated = trunc(number)
|
||||
variance = if number - truncated > 0, do: 1.0, else: 0.0
|
||||
(truncated + variance) / power
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a floating-point value to an arbitrary number of fractional
|
||||
digits (between 0 and 15).
|
||||
Rounds a floating point value to an arbitrary number of fractional digits
|
||||
(between 0 and 15).
|
||||
|
||||
The rounding direction always ties to half up. The operation is
|
||||
performed on the binary floating point, without a conversion to decimal.
|
||||
|
||||
This function only accepts floats and always returns a float. Use
|
||||
`Kernel.round/1` if you want a function that accepts both floats
|
||||
and integers and always returns an integer.
|
||||
|
||||
## Known issues
|
||||
|
||||
The behavior of `round/2` for floats can be surprising. For example:
|
||||
|
||||
iex> Float.round(5.5675, 3)
|
||||
5.567
|
||||
|
||||
One may have expected it to round to the half up 5.568. This is not a bug.
|
||||
Most decimal fractions cannot be represented as a binary floating point
|
||||
and therefore the number above is internally represented as 5.567499999,
|
||||
which explains the behavior above. If you want exact rounding for decimals,
|
||||
you must use a decimal library. The behavior above is also in accordance
|
||||
to reference implementations, such as "Correctly Rounded Binary-Decimal and
|
||||
Decimal-Binary Conversions" by David M. Gay.
|
||||
This function only accepts floats and returns floats. Use `Kernel.round/1`
|
||||
if you want a function that accepts both floats and integers and always
|
||||
returns an integer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.round(12.5)
|
||||
13.0
|
||||
iex> Float.round(5.5674, 3)
|
||||
5.567
|
||||
|
||||
iex> Float.round(5.5675, 3)
|
||||
5.567
|
||||
5.568
|
||||
|
||||
iex> Float.round(-5.5674, 3)
|
||||
-5.567
|
||||
iex> Float.round(-5.5675)
|
||||
-6.0
|
||||
iex> Float.round(12.341444444444441, 15)
|
||||
12.341444444444441
|
||||
iex> Float.round(-0.01)
|
||||
-0.0
|
||||
|
||||
iex> Float.round(-5.5675, 3)
|
||||
-5.568
|
||||
|
||||
"""
|
||||
@spec round(float, precision_range) :: float
|
||||
# This implementation is slow since it relies on big integers.
|
||||
# Faster implementations are available on more recent papers
|
||||
# and could be implemented in the future.
|
||||
def round(float, precision \\ 0)
|
||||
|
||||
def round(float, 0) when float == 0.0, do: float
|
||||
|
||||
def round(float, 0) when is_float(float) do
|
||||
case float |> :erlang.round() |> :erlang.float() do
|
||||
zero when zero == 0.0 and float < 0.0 -> -0.0
|
||||
rounded -> rounded
|
||||
end
|
||||
@spec round(float, 0..15) :: float
|
||||
def round(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
Kernel.round(number * power) / power
|
||||
end
|
||||
|
||||
def round(float, precision) when is_float(float) and precision in @precision_range do
|
||||
round(float, precision, :half_up)
|
||||
end
|
||||
|
||||
def round(float, precision) when is_float(float) do
|
||||
raise ArgumentError, invalid_precision_message(precision)
|
||||
end
|
||||
|
||||
defp round(num, _precision, _rounding) when is_float(num) and num == 0.0, do: num
|
||||
|
||||
defp round(float, precision, rounding) do
|
||||
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
|
||||
{num, count} = decompose(significant, 1)
|
||||
count = count - exp + 1023
|
||||
|
||||
cond do
|
||||
# Precision beyond 15 digits
|
||||
count >= 104 ->
|
||||
case rounding do
|
||||
:ceil when sign === 0 -> 1 / power_of_10(precision)
|
||||
:floor when sign === 1 -> -1 / power_of_10(precision)
|
||||
:ceil when sign === 1 -> minus_zero()
|
||||
:half_up when sign === 1 -> minus_zero()
|
||||
_ -> 0.0
|
||||
end
|
||||
|
||||
# We are asking more precision than we have
|
||||
count <= precision ->
|
||||
float
|
||||
|
||||
true ->
|
||||
# Difference in precision between float and asked precision
|
||||
# We subtract 1 because we need to calculate the remainder too
|
||||
diff = count - precision - 1
|
||||
|
||||
# Get up to latest so we calculate the remainder
|
||||
power_of_10 = power_of_10(diff)
|
||||
|
||||
# Convert the numerand to decimal base
|
||||
num = num * power_of_5(count)
|
||||
|
||||
# Move to the given precision - 1
|
||||
num = div(num, power_of_10)
|
||||
div = div(num, 10)
|
||||
num = rounding(rounding, sign, num, div)
|
||||
|
||||
# Convert back to float without loss
|
||||
# https://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/
|
||||
den = power_of_10(precision)
|
||||
boundary = den <<< 52
|
||||
|
||||
cond do
|
||||
num == 0 and sign == 1 ->
|
||||
minus_zero()
|
||||
|
||||
num == 0 ->
|
||||
0.0
|
||||
|
||||
num >= boundary ->
|
||||
{den, exp} = scale_down(num, boundary, 52)
|
||||
decimal_to_float(sign, num, den, exp)
|
||||
|
||||
true ->
|
||||
{num, exp} = scale_up(num, boundary, 52)
|
||||
decimal_to_float(sign, num, den, exp)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# TODO remove once we require Erlang/OTP 27+
|
||||
# This function tricks the compiler to avoid this bug in previous versions:
|
||||
# https://github.com/elixir-lang/elixir/blob/main/lib/elixir/lib/float.ex#L408-L412
|
||||
defp minus_zero, do: -0.0
|
||||
|
||||
defp decompose(significant, initial) do
|
||||
decompose(significant, 1, 0, initial)
|
||||
end
|
||||
|
||||
defp decompose(<<1::1, bits::bitstring>>, count, last_count, acc) do
|
||||
decompose(bits, count + 1, count, (acc <<< (count - last_count)) + 1)
|
||||
end
|
||||
|
||||
defp decompose(<<0::1, bits::bitstring>>, count, last_count, acc) do
|
||||
decompose(bits, count + 1, last_count, acc)
|
||||
end
|
||||
|
||||
defp decompose(<<>>, _count, last_count, acc) do
|
||||
{acc, last_count}
|
||||
end
|
||||
|
||||
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
|
||||
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
|
||||
|
||||
defp scale_down(num, den, exp) do
|
||||
new_den = den <<< 1
|
||||
|
||||
if num < new_den do
|
||||
{den >>> 52, exp}
|
||||
else
|
||||
scale_down(num, new_den, exp + 1)
|
||||
end
|
||||
end
|
||||
|
||||
defp decimal_to_float(sign, num, den, exp) do
|
||||
quo = div(num, den)
|
||||
rem = num - quo * den
|
||||
|
||||
tmp =
|
||||
case den >>> 1 do
|
||||
den when rem > den -> quo + 1
|
||||
den when rem < den -> quo
|
||||
_ when (quo &&& 1) === 1 -> quo + 1
|
||||
_ -> quo
|
||||
end
|
||||
|
||||
tmp = tmp - @power_of_2_to_52
|
||||
<<tmp::float>> = <<sign::1, exp + 1023::11, tmp::52>>
|
||||
tmp
|
||||
end
|
||||
|
||||
defp rounding(:floor, 1, _num, div), do: div + 1
|
||||
defp rounding(:ceil, 0, _num, div), do: div + 1
|
||||
|
||||
defp rounding(:half_up, _sign, num, div) do
|
||||
case rem(num, 10) do
|
||||
rem when rem < 5 -> div
|
||||
rem when rem >= 5 -> div + 1
|
||||
end
|
||||
end
|
||||
|
||||
defp rounding(_, _, _, div), do: div
|
||||
|
||||
Enum.reduce(0..104, 1, fn x, acc ->
|
||||
Enum.reduce 0..15, 1, fn x, acc ->
|
||||
defp power_of_10(unquote(x)), do: unquote(acc)
|
||||
acc * 10
|
||||
end)
|
||||
|
||||
Enum.reduce(0..104, 1, fn x, acc ->
|
||||
defp power_of_5(unquote(x)), do: unquote(acc)
|
||||
acc * 5
|
||||
end)
|
||||
|
||||
@doc """
|
||||
Returns a pair of integers whose ratio is exactly equal
|
||||
to the original float and with a positive denominator.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.ratio(0.0)
|
||||
{0, 1}
|
||||
iex> Float.ratio(3.14)
|
||||
{7070651414971679, 2251799813685248}
|
||||
iex> Float.ratio(-3.14)
|
||||
{-7070651414971679, 2251799813685248}
|
||||
iex> Float.ratio(1.5)
|
||||
{3, 2}
|
||||
iex> Float.ratio(-1.5)
|
||||
{-3, 2}
|
||||
iex> Float.ratio(16.0)
|
||||
{16, 1}
|
||||
iex> Float.ratio(-16.0)
|
||||
{-16, 1}
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec ratio(float) :: {integer, pos_integer}
|
||||
def ratio(float) when is_float(float) and float == 0.0, do: {0, 1}
|
||||
|
||||
def ratio(float) when is_float(float) do
|
||||
<<sign::1, exp::11, mantissa::52>> = <<float::float>>
|
||||
|
||||
{num, den_exp} =
|
||||
if exp != 0 do
|
||||
# Floats are expressed like this:
|
||||
# (2**52 + mantissa) * 2**(-52 + exp - 1023)
|
||||
#
|
||||
# We compute the root factors of the mantissa so we have this:
|
||||
# (2**52 + mantissa * 2**count) * 2**(-52 + exp - 1023)
|
||||
{mantissa, count} = root_factors(mantissa, 0)
|
||||
|
||||
# Now we can move the count around so we have this:
|
||||
# (2**(52-count) + mantissa) * 2**(count + -52 + exp - 1023)
|
||||
if mantissa == 0 do
|
||||
{1, exp - 1023}
|
||||
else
|
||||
num = (1 <<< (52 - count)) + mantissa
|
||||
den_exp = count - 52 + exp - 1023
|
||||
{num, den_exp}
|
||||
end
|
||||
else
|
||||
# Subnormals are expressed like this:
|
||||
# (mantissa) * 2**(-52 + 1 - 1023)
|
||||
#
|
||||
# So we compute it to this:
|
||||
# (mantissa * 2**(count)) * 2**(-52 + 1 - 1023)
|
||||
#
|
||||
# Which becomes:
|
||||
# mantissa * 2**(count-1074)
|
||||
root_factors(mantissa, -1074)
|
||||
end
|
||||
|
||||
if den_exp > 0 do
|
||||
{sign(sign, num <<< den_exp), 1}
|
||||
else
|
||||
{sign(sign, num), 1 <<< -den_exp}
|
||||
end
|
||||
end
|
||||
|
||||
defp root_factors(mantissa, count) when mantissa != 0 and (mantissa &&& 1) == 0,
|
||||
do: root_factors(mantissa >>> 1, count + 1)
|
||||
|
||||
defp root_factors(mantissa, count),
|
||||
do: {mantissa, count}
|
||||
|
||||
@compile {:inline, sign: 2}
|
||||
defp sign(0, num), do: num
|
||||
defp sign(1, num), do: -num
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the shortest text representation
|
||||
of the given float.
|
||||
|
||||
The underlying algorithm changes depending on the Erlang/OTP version:
|
||||
|
||||
* For OTP >= 24, it uses the algorithm presented in "Ryū: fast
|
||||
float-to-string conversion" in Proceedings of the SIGPLAN '2018
|
||||
Conference on Programming Language Design and Implementation.
|
||||
|
||||
* For OTP < 24, it uses the algorithm presented in "Printing Floating-Point
|
||||
Numbers Quickly and Accurately" in Proceedings of the SIGPLAN '1996
|
||||
Conference on Programming Language Design and Implementation.
|
||||
|
||||
For a configurable representation, use `:erlang.float_to_list/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_charlist(7.0)
|
||||
~c"7.0"
|
||||
|
||||
"""
|
||||
@spec to_charlist(float) :: charlist
|
||||
def to_charlist(float) when is_float(float) do
|
||||
:erlang.float_to_list(float, [:short])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the shortest text representation
|
||||
of the given float.
|
||||
Returns a char list which corresponds to the text representation of the given float.
|
||||
|
||||
The underlying algorithm changes depending on the Erlang/OTP version:
|
||||
|
||||
* For OTP >= 24, it uses the algorithm presented in "Ryū: fast
|
||||
float-to-string conversion" in Proceedings of the SIGPLAN '2018
|
||||
Conference on Programming Language Design and Implementation.
|
||||
|
||||
* For OTP < 24, it uses the algorithm presented in "Printing Floating-Point
|
||||
Numbers Quickly and Accurately" in Proceedings of the SIGPLAN '1996
|
||||
Conference on Programming Language Design and Implementation.
|
||||
|
||||
For a configurable representation, use `:erlang.float_to_binary/2`.
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string(7.0)
|
||||
"7.0"
|
||||
iex> Float.to_char_list(7.0)
|
||||
'7.00000000000000000000e+00'
|
||||
|
||||
"""
|
||||
@spec to_string(float) :: String.t()
|
||||
def to_string(float) when is_float(float) do
|
||||
:erlang.float_to_binary(float, [:short])
|
||||
@spec to_char_list(float) :: char_list
|
||||
def to_char_list(number) do
|
||||
:erlang.float_to_list(number)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Float.to_charlist/1 instead"
|
||||
def to_char_list(float), do: Float.to_charlist(float)
|
||||
@doc """
|
||||
Returns a list which corresponds to the text representation
|
||||
of `float`.
|
||||
|
||||
@doc false
|
||||
@deprecated "Use :erlang.float_to_list/2 instead"
|
||||
## Options
|
||||
|
||||
* `:decimals` — number of decimal points to show
|
||||
* `:scientific` — number of decimal points to show, in scientific format
|
||||
* `:compact` — when true, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_char_list 7.1, [decimals: 2, compact: true]
|
||||
'7.1'
|
||||
|
||||
"""
|
||||
@spec to_char_list(float, list) :: char_list
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use :erlang.float_to_binary/2 instead"
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `some_float`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string(7.0)
|
||||
"7.00000000000000000000e+00"
|
||||
|
||||
"""
|
||||
@spec to_string(float) :: String.t
|
||||
def to_string(some_float) do
|
||||
:erlang.float_to_binary(some_float)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `float`.
|
||||
|
||||
## Options
|
||||
|
||||
* `:decimals` — number of decimal points to show
|
||||
* `:scientific` — number of decimal points to show, in scientific format
|
||||
* `:compact` — when true, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string 7.1, [decimals: 2, compact: true]
|
||||
"7.1"
|
||||
|
||||
"""
|
||||
@spec to_string(float, list) :: String.t
|
||||
def to_string(float, options) do
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
end
|
||||
|
||||
defp invalid_precision_message(precision) do
|
||||
"precision #{precision} is out of valid range of #{inspect(@precision_range)}"
|
||||
end
|
||||
|
||||
defp expand_compact([{:compact, false} | t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true} | t]), do: [:compact | expand_compact(t)]
|
||||
defp expand_compact([h | t]), do: [h | expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
defp expand_compact([{:compact, false}|t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true}|t]), do: [:compact|expand_compact(t)]
|
||||
defp expand_compact([h|t]), do: [h|expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
end
|
||||
|
||||
@@ -1,208 +0,0 @@
|
||||
defmodule Function do
|
||||
@moduledoc """
|
||||
A set of functions for working with functions.
|
||||
|
||||
Anonymous functions are typically created by using `fn`:
|
||||
|
||||
iex> add = fn a, b -> a + b end
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
Anonymous functions can also have multiple clauses. All clauses
|
||||
should expect the same number of arguments:
|
||||
|
||||
iex> negate = fn
|
||||
...> true -> false
|
||||
...> false -> true
|
||||
...> end
|
||||
iex> negate.(false)
|
||||
true
|
||||
|
||||
## The capture operator
|
||||
|
||||
It is also possible to capture public module functions and pass them
|
||||
around as if they were anonymous functions by using the capture
|
||||
operator `&/1`:
|
||||
|
||||
iex> add = &Kernel.+/2
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
iex> length = &String.length/1
|
||||
iex> length.("hello")
|
||||
5
|
||||
|
||||
To capture a definition within the current module, you can skip the
|
||||
module prefix, such as `&my_fun/2`. In those cases, the captured
|
||||
function can be public (`def`) or private (`defp`).
|
||||
|
||||
The capture operator can also be used to create anonymous functions
|
||||
that expect at least one argument:
|
||||
|
||||
iex> add = &(&1 + &2)
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
In such cases, using the capture operator is no different than using `fn`.
|
||||
|
||||
## Internal and external functions
|
||||
|
||||
We say that functions that point to definitions residing in modules, such
|
||||
as `&String.length/1`, are **external** functions. All other functions are
|
||||
**local** and they are always bound to the file or module that defined them.
|
||||
|
||||
Besides the functions in this module to work with functions, `Kernel` also
|
||||
has an `apply/2` function that invokes a function with a dynamic number of
|
||||
arguments, as well as `is_function/1` and `is_function/2`, to check
|
||||
respectively if a given value is a function or a function of a given arity.
|
||||
"""
|
||||
|
||||
@type information ::
|
||||
:arity
|
||||
| :env
|
||||
| :index
|
||||
| :module
|
||||
| :name
|
||||
| :new_index
|
||||
| :new_uniq
|
||||
| :pid
|
||||
| :type
|
||||
| :uniq
|
||||
|
||||
@doc """
|
||||
Captures the given function.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Function.capture(String, :length, 1)
|
||||
&String.length/1
|
||||
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec capture(module, atom, arity) :: fun
|
||||
def capture(module, function_name, arity) do
|
||||
:erlang.make_fun(module, function_name, arity)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list with information about a function.
|
||||
|
||||
The returned keys (with the corresponding possible values) for
|
||||
all types of functions (local and external) are the following:
|
||||
|
||||
* `:type` - `:local` (for anonymous functions) or `:external` (for
|
||||
named functions).
|
||||
|
||||
* `:module` - an atom which is the module where the function is defined when
|
||||
anonymous or the module which the function refers to when it's a named function.
|
||||
|
||||
* `:arity` - (integer) the number of arguments the function is to be called with.
|
||||
|
||||
* `:name` - (atom) the name of the function.
|
||||
|
||||
* `:env` - a list of the environment or free variables. For named
|
||||
functions, the returned list is always empty.
|
||||
|
||||
When `fun` is an anonymous function (that is, the type is `:local`), the following
|
||||
additional keys are returned:
|
||||
|
||||
* `:pid` - PID of the process that originally created the function.
|
||||
|
||||
* `:index` - (integer) an index into the module function table.
|
||||
|
||||
* `:new_index` - (integer) an index into the module function table.
|
||||
|
||||
* `:new_uniq` - (binary) a unique value for this function. It's
|
||||
calculated from the compiled code for the entire module.
|
||||
|
||||
* `:uniq` - (integer) a unique value for this function. This integer is
|
||||
calculated from the compiled code for the entire module.
|
||||
|
||||
**Note**: this function must be used only for debugging purposes.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> fun = fn x -> x end
|
||||
iex> info = Function.info(fun)
|
||||
iex> Keyword.get(info, :arity)
|
||||
1
|
||||
iex> Keyword.get(info, :type)
|
||||
:local
|
||||
|
||||
iex> fun = &String.length/1
|
||||
iex> info = Function.info(fun)
|
||||
iex> Keyword.get(info, :type)
|
||||
:external
|
||||
iex> Keyword.get(info, :name)
|
||||
:length
|
||||
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec info(fun) :: [{information, term}]
|
||||
def info(fun), do: :erlang.fun_info(fun)
|
||||
|
||||
@doc """
|
||||
Returns a specific information about the function.
|
||||
|
||||
The returned information is a two-element tuple in the shape of
|
||||
`{info, value}`.
|
||||
|
||||
For any function, the information asked for can be any of the atoms
|
||||
`:module`, `:name`, `:arity`, `:env`, or `:type`.
|
||||
|
||||
For anonymous functions, there is also information about any of the
|
||||
atoms `:index`, `:new_index`, `:new_uniq`, `:uniq`, and `:pid`.
|
||||
For a named function, the value of any of these items is always the
|
||||
atom `:undefined`.
|
||||
|
||||
For more information on each of the possible returned values, see
|
||||
`info/1`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> f = fn x -> x end
|
||||
iex> Function.info(f, :arity)
|
||||
{:arity, 1}
|
||||
iex> Function.info(f, :type)
|
||||
{:type, :local}
|
||||
|
||||
iex> fun = &String.length/1
|
||||
iex> Function.info(fun, :name)
|
||||
{:name, :length}
|
||||
iex> Function.info(fun, :pid)
|
||||
{:pid, :undefined}
|
||||
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec info(fun, item) :: {item, term} when item: information
|
||||
def info(fun, item), do: :erlang.fun_info(fun, item)
|
||||
|
||||
@doc """
|
||||
Returns its input `value`. This function can be passed as an anonymous function
|
||||
to transformation functions.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Function.identity("Hello world!")
|
||||
"Hello world!"
|
||||
|
||||
iex> ~c"abcdaabccc" |> Enum.sort() |> Enum.chunk_by(&Function.identity/1)
|
||||
[~c"aaa", ~c"bb", ~c"cccc", ~c"d"]
|
||||
|
||||
iex> Enum.group_by(~c"abracadabra", &Function.identity/1)
|
||||
%{97 => ~c"aaaaa", 98 => ~c"bb", 99 => ~c"c", 100 => ~c"d", 114 => ~c"rr"}
|
||||
|
||||
iex> Enum.map([1, 2, 3, 4], &Function.identity/1)
|
||||
[1, 2, 3, 4]
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec identity(value) :: value when value: var
|
||||
def identity(value), do: value
|
||||
end
|
||||
+436
-291
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,19 @@
|
||||
defmodule GenEvent.Stream do
|
||||
@moduledoc false
|
||||
@moduledoc """
|
||||
Defines a `GenEvent` stream.
|
||||
|
||||
This is a struct returned by `stream/2`. The struct is public and
|
||||
contains the following fields:
|
||||
|
||||
* `:manager` - the manager reference given to `GenEvent.stream/2`
|
||||
* `:timeout` - the timeout in between events, defaults to `:infinity`
|
||||
|
||||
"""
|
||||
defstruct manager: nil, timeout: :infinity
|
||||
|
||||
@type t :: %__MODULE__{manager: GenEvent.manager(), timeout: timeout}
|
||||
@type t :: %__MODULE__{
|
||||
manager: GenEvent.manager,
|
||||
timeout: timeout}
|
||||
|
||||
@doc false
|
||||
def init({_pid, _ref} = state) do
|
||||
@@ -11,22 +22,12 @@ defmodule GenEvent.Stream do
|
||||
|
||||
@doc false
|
||||
def handle_event(event, _state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit({:bad_event, event})
|
||||
1 -> :remove_handler
|
||||
end
|
||||
exit({:bad_event, event})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
1 -> {:remove_handler, reason}
|
||||
end
|
||||
exit({:bad_call, msg})
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -47,7 +48,7 @@ end
|
||||
|
||||
defimpl Enumerable, for: GenEvent.Stream do
|
||||
def reduce(stream, acc, fun) do
|
||||
start_fun = fn -> start(stream) end
|
||||
start_fun = fn() -> start(stream) end
|
||||
next_fun = &next(stream, &1)
|
||||
stop_fun = &stop(stream, &1)
|
||||
Stream.resource(start_fun, next_fun, stop_fun).(acc, wrap_reducer(fun))
|
||||
@@ -61,33 +62,26 @@ defimpl Enumerable, for: GenEvent.Stream do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
def slice(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
defp wrap_reducer(fun) do
|
||||
fn
|
||||
{:ack, manager, ref, event}, acc ->
|
||||
send(manager, {ref, :ok})
|
||||
send manager, {ref, :ok}
|
||||
fun.(event, acc)
|
||||
|
||||
{:async, _manager, _ref, event}, acc ->
|
||||
fun.(event, acc)
|
||||
|
||||
{:sync, manager, ref, event}, acc ->
|
||||
try do
|
||||
fun.(event, acc)
|
||||
after
|
||||
send(manager, {ref, :ok})
|
||||
send manager, {ref, :ok}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp start(%{manager: manager} = stream) do
|
||||
try do
|
||||
{:ok, {pid, ref}} =
|
||||
:gen.call(manager, self(), {:add_process_handler, self(), self()}, :infinity)
|
||||
|
||||
{:ok, {pid, ref}} = :gen.call(manager, self(),
|
||||
{:add_process_handler, self(), self()}, :infinity)
|
||||
mon_ref = Process.monitor(pid)
|
||||
{pid, ref, mon_ref}
|
||||
catch
|
||||
@@ -135,7 +129,6 @@ defimpl Enumerable, for: GenEvent.Stream do
|
||||
case wait_for_handler_removal(pid, ref, mon_ref) do
|
||||
:ok ->
|
||||
flush_events(ref)
|
||||
|
||||
{:error, reason} ->
|
||||
exit({reason, {__MODULE__, :stop, [stream, acc]}})
|
||||
end
|
||||
@@ -144,16 +137,21 @@ defimpl Enumerable, for: GenEvent.Stream do
|
||||
# If we reach this branch, the handler was not removed yet,
|
||||
# so we trigger a request for doing so.
|
||||
defp stop(stream, {pid, ref, _} = acc) do
|
||||
_ = :gen_event.delete_handler(pid, {pid, ref}, :shutdown)
|
||||
_ = GenEvent.remove_handler(pid, {pid, ref}, :shutdown)
|
||||
stop(stream, {:removed, acc})
|
||||
end
|
||||
|
||||
defp wait_for_handler_removal(pid, ref, mon_ref) do
|
||||
receive do
|
||||
{:gen_event_EXIT, {^pid, ^ref}, _reason} ->
|
||||
{:gen_event_EXIT, {^pid, ^ref}, reason}
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 3 and elem(reason, 0) == :swapped ->
|
||||
Process.demonitor(mon_ref, [:flush])
|
||||
:ok
|
||||
|
||||
{:gen_event_EXIT, {^pid, ^ref}, reason} ->
|
||||
Process.demonitor(mon_ref, [:flush])
|
||||
{:error, reason}
|
||||
{:DOWN, ^mon_ref, _, _, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
@@ -161,8 +159,7 @@ defimpl Enumerable, for: GenEvent.Stream do
|
||||
|
||||
defp flush_events(ref) do
|
||||
receive do
|
||||
{_from, {_pid, ^ref}, {notify, _event}}
|
||||
when notify in [:notify, :ack_notify, :sync_notify] ->
|
||||
{_from, {_pid, ^ref}, {notify, _event}} when notify in [:notify, :ack_notify, :sync_notify] ->
|
||||
flush_events(ref)
|
||||
after
|
||||
0 -> :ok
|
||||
|
||||
+238
-1079
File diff suppressed because it is too large
Load Diff
+66
-108
@@ -1,12 +1,19 @@
|
||||
defmodule HashDict do
|
||||
@moduledoc """
|
||||
Tuple-based HashDict implementation.
|
||||
A key-value store.
|
||||
|
||||
This module is deprecated. Use the `Map` module instead.
|
||||
The `HashDict` is represented internally as a struct, therefore
|
||||
`%HashDict{}` can be used whenever there is a need to match
|
||||
on any `HashDict`. Note though the struct fields are private and
|
||||
must not be accessed directly. Instead, use the functions on this
|
||||
or in the `Dict` module.
|
||||
|
||||
Implementation-wise, `HashDict` is implemented using tries, which
|
||||
grows in space as the number of keys grows, working well with both
|
||||
small and large set of keys. For more information about the
|
||||
functions and their APIs, please consult the `Dict` module.
|
||||
"""
|
||||
|
||||
@moduledoc deprecated: "Use Map instead"
|
||||
|
||||
use Dict
|
||||
|
||||
@node_bitmap 0b111
|
||||
@@ -22,70 +29,58 @@ defmodule HashDict do
|
||||
@compile :inline_list_funcs
|
||||
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
|
||||
|
||||
message = "Use maps and the Map module instead"
|
||||
|
||||
@doc """
|
||||
Creates a new empty dict.
|
||||
"""
|
||||
@spec new :: Dict.t()
|
||||
@deprecated message
|
||||
@spec new :: Dict.t
|
||||
def new do
|
||||
%HashDict{}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def put(%HashDict{root: root, size: size}, key, value) do
|
||||
{root, counter} = do_put(root, key, value, key_hash(key))
|
||||
%HashDict{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update!(%HashDict{root: root, size: size} = dict, key, fun) when is_function(fun, 1) do
|
||||
{root, counter} =
|
||||
do_update(root, key, fn -> raise KeyError, key: key, term: dict end, fun, key_hash(key))
|
||||
|
||||
{root, counter} = do_update(root, key, fn -> raise KeyError, key: key, term: dict end,
|
||||
fun, key_hash(key))
|
||||
%HashDict{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update(%HashDict{root: root, size: size}, key, default, fun) when is_function(fun, 1) do
|
||||
{root, counter} = do_update(root, key, fn -> default end, fun, key_hash(key))
|
||||
def update(%HashDict{root: root, size: size}, key, initial, fun) when is_function(fun, 1) do
|
||||
{root, counter} = do_update(root, key, fn -> initial end, fun, key_hash(key))
|
||||
%HashDict{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def fetch(%HashDict{root: root}, key) do
|
||||
do_fetch(root, key, key_hash(key))
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def delete(dict, key) do
|
||||
case dict_delete(dict, key) do
|
||||
{dict, _value} -> dict
|
||||
:error -> dict
|
||||
:error -> dict
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def pop(dict, key, default \\ nil) do
|
||||
case dict_delete(dict, key) do
|
||||
{dict, value} -> {value, dict}
|
||||
:error -> {default, dict}
|
||||
:error -> {default, dict}
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def size(%HashDict{size: size}) do
|
||||
size
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated message
|
||||
def reduce(%HashDict{root: root}, acc, fun) do
|
||||
do_reduce(root, acc, fun, @node_size, fn
|
||||
{:suspend, acc} -> {:suspended, acc, &{:done, elem(&1, 1)}}
|
||||
{:halt, acc} -> {:halted, acc}
|
||||
{:cont, acc} -> {:done, acc}
|
||||
{:halt, acc} -> {:halted, acc}
|
||||
{:cont, acc} -> {:done, acc}
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -95,7 +90,7 @@ defmodule HashDict do
|
||||
def dict_delete(%HashDict{root: root, size: size}, key) do
|
||||
case do_delete(root, key, key_hash(key)) do
|
||||
{root, value} -> {%HashDict{root: root, size: size - 1}, value}
|
||||
:error -> :error
|
||||
:error -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@@ -103,105 +98,86 @@ defmodule HashDict do
|
||||
|
||||
defp do_fetch(node, key, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[^key | v] -> {:ok, v}
|
||||
[^key|v] -> {:ok, v}
|
||||
{^key, v, _} -> {:ok, v}
|
||||
{_, _, n} -> do_fetch(n, key, key_shift(hash))
|
||||
_ -> :error
|
||||
{_, _, n} -> do_fetch(n, key, key_shift(hash))
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp do_put(node, key, value, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | value]), 1}
|
||||
|
||||
[^key | _] ->
|
||||
{put_elem(node, index, [key | value]), 0}
|
||||
|
||||
[k | v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key | value])
|
||||
{put_elem(node, index, [key|value]), 1}
|
||||
[^key|_] ->
|
||||
{put_elem(node, index, [key|value]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|value])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
|
||||
{^key, _, n} ->
|
||||
{put_elem(node, index, {key, value, n}), 0}
|
||||
|
||||
{k, v, n} ->
|
||||
{n, counter} = do_put(n, key, value, key_shift(hash))
|
||||
{put_elem(node, index, {k, v, n}), counter}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_update(node, key, default, fun, hash) do
|
||||
defp do_update(node, key, initial, fun, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | default.()]), 1}
|
||||
|
||||
[^key | value] ->
|
||||
{put_elem(node, index, [key | fun.(value)]), 0}
|
||||
|
||||
[k | v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key | default.()])
|
||||
{put_elem(node, index, [key|initial.()]), 1}
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, [key|fun.(value)]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|initial.()])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, {key, fun.(value), n}), 0}
|
||||
|
||||
{k, v, n} ->
|
||||
{n, counter} = do_update(n, key, default, fun, key_shift(hash))
|
||||
{n, counter} = do_update(n, key, initial, fun, key_shift(hash))
|
||||
{put_elem(node, index, {k, v, n}), counter}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_delete(node, key, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
:error
|
||||
|
||||
[^key | value] ->
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, []), value}
|
||||
|
||||
[_ | _] ->
|
||||
[_|_] ->
|
||||
:error
|
||||
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, do_compact_node(n)), value}
|
||||
|
||||
{k, v, n} ->
|
||||
case do_delete(n, key, key_shift(hash)) do
|
||||
{@node_template, value} ->
|
||||
{put_elem(node, index, [k | v]), value}
|
||||
|
||||
{put_elem(node, index, [k|v]), value}
|
||||
{n, value} ->
|
||||
{put_elem(node, index, {k, v, n}), value}
|
||||
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each(0..(@node_size - 1), fn index ->
|
||||
Enum.each 0..(@node_size - 1), fn index ->
|
||||
defp do_compact_node(node) when elem(node, unquote(index)) != [] do
|
||||
case elem(node, unquote(index)) do
|
||||
[k | v] ->
|
||||
[k|v] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [k | v]
|
||||
@node_template -> [k|v]
|
||||
n -> {k, v, n}
|
||||
end
|
||||
|
||||
{k, v, n} ->
|
||||
{k, v, put_elem(node, unquote(index), do_compact_node(n))}
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
## Dict reduce
|
||||
|
||||
@@ -217,8 +193,8 @@ defmodule HashDict do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
defp do_reduce_each([k | v], {:cont, acc}, fun, next) do
|
||||
next.(fun.({k, v}, acc))
|
||||
defp do_reduce_each([k|v], {:cont, acc}, fun, next) do
|
||||
next.(fun.({k,v}, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each({k, v, n}, {:cont, acc}, fun, next) do
|
||||
@@ -226,12 +202,7 @@ defmodule HashDict do
|
||||
end
|
||||
|
||||
defp do_reduce(node, acc, fun, count, next) when count > 0 do
|
||||
do_reduce_each(
|
||||
:erlang.element(count, node),
|
||||
acc,
|
||||
fun,
|
||||
&do_reduce(node, &1, fun, count - 1, next)
|
||||
)
|
||||
do_reduce_each(:erlang.element(count, node), acc, fun, &do_reduce(node, &1, fun, count - 1, next))
|
||||
end
|
||||
|
||||
defp do_reduce(_node, acc, _fun, 0, next) do
|
||||
@@ -256,45 +227,34 @@ defmodule HashDict do
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: HashDict do
|
||||
def reduce(dict, acc, fun) do
|
||||
# Avoid warnings about HashDict being deprecated.
|
||||
module = String.to_atom("HashDict")
|
||||
module.reduce(dict, acc, fun)
|
||||
def reduce(dict, acc, fun), do: HashDict.reduce(dict, acc, fun)
|
||||
def member?(dict, {k, v}), do: {:ok, match?({:ok, ^v}, HashDict.fetch(dict, k))}
|
||||
def member?(_dict, _), do: {:ok, false}
|
||||
def count(dict), do: {:ok, HashDict.size(dict)}
|
||||
end
|
||||
|
||||
defimpl Access, for: HashDict do
|
||||
def get(dict, key) do
|
||||
HashDict.get(dict, key, nil)
|
||||
end
|
||||
|
||||
def member?(dict, {key, value}) do
|
||||
# Avoid warnings about HashDict being deprecated.
|
||||
module = String.to_atom("HashDict")
|
||||
{:ok, match?({:ok, ^value}, module.fetch(dict, key))}
|
||||
end
|
||||
|
||||
def member?(_dict, _) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
def count(dict) do
|
||||
# Avoid warnings about HashDict being deprecated.
|
||||
module = String.to_atom("HashDict")
|
||||
{:ok, module.size(dict)}
|
||||
end
|
||||
|
||||
def slice(_dict) do
|
||||
{:error, __MODULE__}
|
||||
def get_and_update(dict, key, fun) do
|
||||
{get, update} = fun.(HashDict.get(dict, key, nil))
|
||||
{get, HashDict.put(dict, key, update)}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: HashDict do
|
||||
def into(original) do
|
||||
# Avoid warnings about HashDict being deprecated.
|
||||
module = String.to_atom("HashDict")
|
||||
def empty(_dict) do
|
||||
HashDict.new
|
||||
end
|
||||
|
||||
collector_fun = fn
|
||||
dict, {:cont, {key, value}} -> module.put(dict, key, value)
|
||||
def into(original) do
|
||||
{original, fn
|
||||
dict, {:cont, {k, v}} -> Dict.put(dict, k, v)
|
||||
dict, :done -> dict
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
{original, collector_fun}
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -302,8 +262,6 @@ defimpl Inspect, for: HashDict do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(dict, opts) do
|
||||
# Avoid warnings about HashDict being deprecated.
|
||||
module = String.to_atom("HashDict")
|
||||
concat(["#HashDict<", Inspect.List.inspect(module.to_list(dict), opts), ">"])
|
||||
concat ["#HashDict<", Inspect.List.inspect(HashDict.to_list(dict), opts), ">"]
|
||||
end
|
||||
end
|
||||
|
||||
+73
-117
@@ -1,19 +1,26 @@
|
||||
defmodule HashSet do
|
||||
@moduledoc """
|
||||
Tuple-based HashSet implementation.
|
||||
A set store.
|
||||
|
||||
This module is deprecated. Use the `MapSet` module instead.
|
||||
The `HashSet` is represented internally as a struct, therefore
|
||||
`%HashSet{}` can be used whenever there is a need to match
|
||||
on any `HashSet`. Note though the struct fields are private and
|
||||
must not be accessed directly. Instead, use the functions on this
|
||||
or in the `Set` module.
|
||||
|
||||
The `HashSet` is implemented using tries, which grows in
|
||||
space as the number of keys grows, working well with both
|
||||
small and large set of keys. For more information about the
|
||||
functions and their APIs, please consult the `Set` module.
|
||||
"""
|
||||
|
||||
@moduledoc deprecated: "Use MapSet instead"
|
||||
@behaviour Set
|
||||
|
||||
@node_bitmap 0b111
|
||||
@node_shift 3
|
||||
@node_size 8
|
||||
@node_template :erlang.make_tuple(@node_size, [])
|
||||
|
||||
message = "Use the MapSet module instead"
|
||||
|
||||
@opaque t :: %__MODULE__{size: non_neg_integer, root: term}
|
||||
@doc false
|
||||
defstruct size: 0, root: @node_template
|
||||
@@ -22,85 +29,74 @@ defmodule HashSet do
|
||||
@compile :inline_list_funcs
|
||||
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
|
||||
|
||||
@deprecated message
|
||||
@spec new :: Set.t()
|
||||
@doc """
|
||||
Creates a new empty set.
|
||||
"""
|
||||
@spec new :: Set.t
|
||||
def new do
|
||||
%HashSet{}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def union(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) when size1 <= size2 do
|
||||
set_fold(set1, set2, fn v, acc -> put(acc, v) end)
|
||||
set_fold set1, set2, fn v, acc -> put(acc, v) end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def union(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold(set2, set1, fn v, acc -> put(acc, v) end)
|
||||
set_fold set2, set1, fn v, acc -> put(acc, v) end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def intersection(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold(set1, %HashSet{}, fn v, acc ->
|
||||
set_fold set1, %HashSet{}, fn v, acc ->
|
||||
if member?(set2, v), do: put(acc, v), else: acc
|
||||
end)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def difference(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold(set2, set1, fn v, acc -> delete(acc, v) end)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def to_list(set) do
|
||||
set_fold(set, [], &[&1 | &2]) |> :lists.reverse()
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def equal?(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) do
|
||||
case size1 do
|
||||
^size2 -> subset?(set1, set2)
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def difference(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold set2, set1, fn v, acc -> delete(acc, v) end
|
||||
end
|
||||
|
||||
def to_list(set) do
|
||||
set_fold(set, [], &[&1|&2]) |> :lists.reverse
|
||||
end
|
||||
|
||||
def equal?(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) do
|
||||
case size1 do
|
||||
^size2 -> subset?(set1, set2)
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
def subset?(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
reduce(set1, {:cont, true}, fn member, acc ->
|
||||
case member?(set2, member) do
|
||||
true -> {:cont, acc}
|
||||
_ -> {:halt, false}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end)
|
||||
|> elem(1)
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def disjoint?(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
reduce(set2, {:cont, true}, fn member, acc ->
|
||||
case member?(set1, member) do
|
||||
false -> {:cont, acc}
|
||||
_ -> {:halt, false}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end)
|
||||
|> elem(1)
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def member?(%HashSet{root: root}, term) do
|
||||
do_member?(root, term, key_hash(term))
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def put(%HashSet{root: root, size: size}, term) do
|
||||
{root, counter} = do_put(root, term, key_hash(term))
|
||||
%HashSet{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def delete(%HashSet{root: root, size: size} = set, term) do
|
||||
case do_delete(root, term, key_hash(term)) do
|
||||
{:ok, root} -> %HashSet{root: root, size: size - 1}
|
||||
:error -> set
|
||||
:error -> set
|
||||
end
|
||||
end
|
||||
|
||||
@@ -108,12 +104,11 @@ defmodule HashSet do
|
||||
def reduce(%HashSet{root: root}, acc, fun) do
|
||||
do_reduce(root, acc, fun, @node_size, fn
|
||||
{:suspend, acc} -> {:suspended, acc, &{:done, elem(&1, 1)}}
|
||||
{:halt, acc} -> {:halted, acc}
|
||||
{:cont, acc} -> {:done, acc}
|
||||
{:halt, acc} -> {:halted, acc}
|
||||
{:cont, acc} -> {:done, acc}
|
||||
end)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def size(%HashSet{size: size}) do
|
||||
size
|
||||
end
|
||||
@@ -128,85 +123,72 @@ defmodule HashSet do
|
||||
|
||||
defp do_member?(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] -> false
|
||||
[^term | _] -> true
|
||||
[_] -> false
|
||||
[_ | n] -> do_member?(n, term, key_shift(hash))
|
||||
[] -> false
|
||||
[^term|_] -> true
|
||||
[_] -> false
|
||||
[_|n] -> do_member?(n, term, key_shift(hash))
|
||||
end
|
||||
end
|
||||
|
||||
defp do_put(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [term]), 1}
|
||||
|
||||
[^term | _] ->
|
||||
[^term|_] ->
|
||||
{node, 0}
|
||||
|
||||
[t] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [term])
|
||||
{put_elem(node, index, [t | n]), 1}
|
||||
|
||||
[t | n] ->
|
||||
{put_elem(node, index, [t|n]), 1}
|
||||
[t|n] ->
|
||||
{n, counter} = do_put(n, term, key_shift(hash))
|
||||
{put_elem(node, index, [t | n]), counter}
|
||||
{put_elem(node, index, [t|n]), counter}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_delete(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
:error
|
||||
|
||||
[^term] ->
|
||||
{:ok, put_elem(node, index, [])}
|
||||
|
||||
[_] ->
|
||||
:error
|
||||
|
||||
[^term | n] ->
|
||||
[^term|n] ->
|
||||
{:ok, put_elem(node, index, do_compact_node(n))}
|
||||
|
||||
[t | n] ->
|
||||
[t|n] ->
|
||||
case do_delete(n, term, key_shift(hash)) do
|
||||
{:ok, @node_template} ->
|
||||
{:ok, put_elem(node, index, [t])}
|
||||
|
||||
{:ok, n} ->
|
||||
{:ok, put_elem(node, index, [t | n])}
|
||||
|
||||
{:ok, put_elem(node, index, [t|n])}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each(0..(@node_size - 1), fn index ->
|
||||
Enum.each 0..(@node_size - 1), fn index ->
|
||||
defp do_compact_node(node) when elem(node, unquote(index)) != [] do
|
||||
case elem(node, unquote(index)) do
|
||||
[t] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [t]
|
||||
n -> [t | n]
|
||||
n -> [t|n]
|
||||
end
|
||||
|
||||
[t | n] ->
|
||||
[t | put_elem(node, unquote(index), do_compact_node(n))]
|
||||
[t|n] ->
|
||||
[t|put_elem(node, unquote(index), do_compact_node(n))]
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
## Set fold
|
||||
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t | n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t|n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
|
||||
defp do_fold(node, acc, fun, count) when count > 0 do
|
||||
acc = do_fold_each(:erlang.element(count, node), acc, fun)
|
||||
@@ -235,17 +217,12 @@ defmodule HashSet do
|
||||
next.(fun.(t, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each([t | n], {:cont, acc}, fun, next) do
|
||||
defp do_reduce_each([t|n], {:cont, acc}, fun, next) do
|
||||
do_reduce(n, fun.(t, acc), fun, @node_size, next)
|
||||
end
|
||||
|
||||
defp do_reduce(node, acc, fun, count, next) when count > 0 do
|
||||
do_reduce_each(
|
||||
:erlang.element(count, node),
|
||||
acc,
|
||||
fun,
|
||||
&do_reduce(node, &1, fun, count - 1, next)
|
||||
)
|
||||
do_reduce_each(:erlang.element(count, node), acc, fun, &do_reduce(node, &1, fun, count - 1, next))
|
||||
end
|
||||
|
||||
defp do_reduce(_node, acc, _fun, 0, next) do
|
||||
@@ -270,41 +247,22 @@ defmodule HashSet do
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: HashSet do
|
||||
def reduce(set, acc, fun) do
|
||||
# Avoid warnings about HashSet being deprecated.
|
||||
module = String.to_atom("HashSet")
|
||||
module.reduce(set, acc, fun)
|
||||
end
|
||||
|
||||
def member?(set, term) do
|
||||
# Avoid warnings about HashSet being deprecated.
|
||||
module = String.to_atom("HashSet")
|
||||
{:ok, module.member?(set, term)}
|
||||
end
|
||||
|
||||
def count(set) do
|
||||
# Avoid warnings about HashSet being deprecated.
|
||||
module = String.to_atom("HashSet")
|
||||
{:ok, module.size(set)}
|
||||
end
|
||||
|
||||
def slice(_set) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
def reduce(set, acc, fun), do: HashSet.reduce(set, acc, fun)
|
||||
def member?(set, v), do: {:ok, HashSet.member?(set, v)}
|
||||
def count(set), do: {:ok, HashSet.size(set)}
|
||||
end
|
||||
|
||||
defimpl Collectable, for: HashSet do
|
||||
def into(original) do
|
||||
# Avoid warnings about HashSet being deprecated.
|
||||
module = String.to_atom("HashSet")
|
||||
def empty(_dict) do
|
||||
HashSet.new
|
||||
end
|
||||
|
||||
collector_fun = fn
|
||||
set, {:cont, term} -> module.put(set, term)
|
||||
def into(original) do
|
||||
{original, fn
|
||||
set, {:cont, x} -> HashSet.put(set, x)
|
||||
set, :done -> set
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
{original, collector_fun}
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -312,8 +270,6 @@ defimpl Inspect, for: HashSet do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(set, opts) do
|
||||
# Avoid warnings about HashSet being deprecated.
|
||||
module = String.to_atom("HashSet")
|
||||
concat(["#HashSet<", Inspect.List.inspect(module.to_list(set), opts), ">"])
|
||||
concat ["#HashSet<", Inspect.List.inspect(HashSet.to_list(set), opts), ">"]
|
||||
end
|
||||
end
|
||||
|
||||
+349
-496
File diff suppressed because it is too large
Load Diff
+295
-908
File diff suppressed because it is too large
Load Diff
+55
-403
@@ -1,323 +1,80 @@
|
||||
defmodule Integer do
|
||||
@moduledoc """
|
||||
Functions for working with integers.
|
||||
|
||||
Some functions that work on integers are found in `Kernel`:
|
||||
|
||||
* `Kernel.abs/1`
|
||||
* `Kernel.div/2`
|
||||
* `Kernel.max/2`
|
||||
* `Kernel.min/2`
|
||||
* `Kernel.rem/2`
|
||||
|
||||
"""
|
||||
|
||||
import Bitwise
|
||||
|
||||
@doc """
|
||||
Determines if `integer` is odd.
|
||||
Determines if an integer is odd.
|
||||
|
||||
Returns `true` if the given `integer` is an odd number,
|
||||
otherwise it returns `false`.
|
||||
Returns `true` if `n` is an odd number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_odd(5)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(6)
|
||||
false
|
||||
|
||||
iex> Integer.is_odd(-5)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(0)
|
||||
false
|
||||
|
||||
"""
|
||||
defguard is_odd(integer) when is_integer(integer) and (integer &&& 1) == 1
|
||||
defmacro is_odd(n) do
|
||||
quote do: (unquote(n) &&& 1) == 1
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines if an `integer` is even.
|
||||
Determines if an integer is even.
|
||||
|
||||
Returns `true` if the given `integer` is an even number,
|
||||
otherwise it returns `false`.
|
||||
Returns `true` if `n` is an even number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_even(10)
|
||||
true
|
||||
|
||||
iex> Integer.is_even(5)
|
||||
false
|
||||
|
||||
iex> Integer.is_even(-10)
|
||||
true
|
||||
|
||||
iex> Integer.is_even(0)
|
||||
true
|
||||
|
||||
"""
|
||||
defguard is_even(integer) when is_integer(integer) and (integer &&& 1) == 0
|
||||
|
||||
@doc """
|
||||
Computes `base` raised to power of `exponent`.
|
||||
|
||||
Both `base` and `exponent` must be integers.
|
||||
The exponent must be zero or positive.
|
||||
|
||||
See `Float.pow/2` for exponentiation of negative
|
||||
exponents as well as floats.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.pow(2, 0)
|
||||
1
|
||||
iex> Integer.pow(2, 1)
|
||||
2
|
||||
iex> Integer.pow(2, 10)
|
||||
1024
|
||||
iex> Integer.pow(2, 11)
|
||||
2048
|
||||
iex> Integer.pow(2, 64)
|
||||
0x10000000000000000
|
||||
|
||||
iex> Integer.pow(3, 4)
|
||||
81
|
||||
iex> Integer.pow(4, 3)
|
||||
64
|
||||
|
||||
iex> Integer.pow(-2, 3)
|
||||
-8
|
||||
iex> Integer.pow(-2, 4)
|
||||
16
|
||||
|
||||
iex> Integer.pow(2, -2)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec pow(integer, non_neg_integer) :: integer
|
||||
def pow(base, exponent) when is_integer(base) and is_integer(exponent) do
|
||||
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
|
||||
base ** exponent
|
||||
defmacro is_even(n) do
|
||||
quote do: (unquote(n) &&& 1) == 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
Computes the modulo remainder of an integer division.
|
||||
Converts a binary to an integer.
|
||||
|
||||
This function performs a [floored division](`floor_div/2`), which means that
|
||||
the result will always have the sign of the `divisor`.
|
||||
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
integer, or when the `divisor` is `0`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.mod(5, 2)
|
||||
1
|
||||
iex> Integer.mod(6, -4)
|
||||
-2
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec mod(integer, neg_integer | pos_integer) :: integer
|
||||
def mod(dividend, divisor) do
|
||||
remainder = rem(dividend, divisor)
|
||||
|
||||
if remainder * divisor < 0 do
|
||||
remainder + divisor
|
||||
else
|
||||
remainder
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a floored integer division.
|
||||
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
integer, or when the `divisor` is `0`.
|
||||
|
||||
This function performs a *floored* integer division, which means that
|
||||
the result will always be rounded towards negative infinity.
|
||||
|
||||
If you want to perform truncated integer division (rounding towards zero),
|
||||
use `Kernel.div/2` instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.floor_div(5, 2)
|
||||
2
|
||||
iex> Integer.floor_div(6, -4)
|
||||
-2
|
||||
iex> Integer.floor_div(-99, 2)
|
||||
-50
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec floor_div(integer, neg_integer | pos_integer) :: integer
|
||||
def floor_div(dividend, divisor) do
|
||||
if :erlang.xor(dividend < 0, divisor < 0) and rem(dividend, divisor) != 0 do
|
||||
div(dividend, divisor) - 1
|
||||
else
|
||||
div(dividend, divisor)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the ordered digits for the given `integer`.
|
||||
|
||||
An optional `base` value may be provided representing the radix for the returned
|
||||
digits. This one must be an integer >= 2.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.digits(123)
|
||||
[1, 2, 3]
|
||||
|
||||
iex> Integer.digits(170, 2)
|
||||
[1, 0, 1, 0, 1, 0, 1, 0]
|
||||
|
||||
iex> Integer.digits(-170, 2)
|
||||
[-1, 0, -1, 0, -1, 0, -1, 0]
|
||||
|
||||
"""
|
||||
@spec digits(integer, pos_integer) :: [integer, ...]
|
||||
def digits(integer, base \\ 10)
|
||||
when is_integer(integer) and is_integer(base) and base >= 2 do
|
||||
do_digits(integer, base, [])
|
||||
end
|
||||
|
||||
defp do_digits(integer, base, acc) when abs(integer) < base, do: [integer | acc]
|
||||
|
||||
defp do_digits(integer, base, acc),
|
||||
do: do_digits(div(integer, base), base, [rem(integer, base) | acc])
|
||||
|
||||
@doc """
|
||||
Returns the integer represented by the ordered `digits`.
|
||||
|
||||
An optional `base` value may be provided representing the radix for the `digits`.
|
||||
Base has to be an integer greater than or equal to `2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.undigits([1, 2, 3])
|
||||
123
|
||||
|
||||
iex> Integer.undigits([1, 4], 16)
|
||||
20
|
||||
|
||||
iex> Integer.undigits([])
|
||||
0
|
||||
|
||||
"""
|
||||
@spec undigits([integer], pos_integer) :: integer
|
||||
def undigits(digits, base \\ 10) when is_list(digits) and is_integer(base) and base >= 2 do
|
||||
do_undigits(digits, base, 0)
|
||||
end
|
||||
|
||||
defp do_undigits([], _base, acc), do: acc
|
||||
|
||||
defp do_undigits([digit | _], base, _) when is_integer(digit) and digit >= base,
|
||||
do: raise(ArgumentError, "invalid digit #{digit} in base #{base}")
|
||||
|
||||
defp do_undigits([digit | tail], base, acc) when is_integer(digit),
|
||||
do: do_undigits(tail, base, acc * base + digit)
|
||||
|
||||
@doc """
|
||||
Parses a text representation of an integer.
|
||||
|
||||
An optional `base` to the corresponding integer can be provided.
|
||||
If `base` is not given, 10 will be used.
|
||||
|
||||
If successful, returns a tuple in the form of `{integer, remainder_of_binary}`.
|
||||
If successful, returns a tuple of the form `{integer, remainder_of_binary}`.
|
||||
Otherwise `:error`.
|
||||
|
||||
Raises an error if `base` is less than 2 or more than 36.
|
||||
|
||||
If you want to convert a string-formatted integer directly to an integer,
|
||||
`String.to_integer/1` or `String.to_integer/2` can be used instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.parse("34")
|
||||
{34, ""}
|
||||
{34,""}
|
||||
|
||||
iex> Integer.parse("34.5")
|
||||
{34, ".5"}
|
||||
{34,".5"}
|
||||
|
||||
iex> Integer.parse("three")
|
||||
:error
|
||||
|
||||
iex> Integer.parse("34", 10)
|
||||
{34, ""}
|
||||
|
||||
iex> Integer.parse("f4", 16)
|
||||
{244, ""}
|
||||
|
||||
iex> Integer.parse("Awww++", 36)
|
||||
{509216, "++"}
|
||||
|
||||
iex> Integer.parse("fab", 10)
|
||||
:error
|
||||
|
||||
iex> Integer.parse("a2", 38)
|
||||
** (ArgumentError) invalid base 38
|
||||
|
||||
"""
|
||||
@spec parse(binary, 2..36) :: {integer, remainder_of_binary :: binary} | :error
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse(_binary, base) when base not in 2..36 do
|
||||
raise ArgumentError, "invalid base #{inspect(base)}"
|
||||
end
|
||||
|
||||
def parse(binary, base) when is_binary(binary) do
|
||||
case count_digits(binary, base) do
|
||||
0 ->
|
||||
:error
|
||||
|
||||
count ->
|
||||
{digits, rem} = :erlang.split_binary(binary, count)
|
||||
{:erlang.binary_to_integer(digits, base), rem}
|
||||
@spec parse(binary) :: {integer, binary} | :error
|
||||
def parse(<< ?-, bin :: binary >>) do
|
||||
case do_parse(bin) do
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
defp count_digits(<<sign, rest::bits>>, base) when sign in ~c"+-" do
|
||||
case count_digits_nosign(rest, base, 1) do
|
||||
1 -> 0
|
||||
count -> count
|
||||
end
|
||||
def parse(<< ?+, bin :: binary >>) do
|
||||
do_parse(bin)
|
||||
end
|
||||
|
||||
defp count_digits(<<rest::bits>>, base) do
|
||||
count_digits_nosign(rest, base, 0)
|
||||
def parse(bin) when is_binary(bin) do
|
||||
do_parse(bin)
|
||||
end
|
||||
|
||||
digits = [{?0..?9, -?0}, {?A..?Z, 10 - ?A}, {?a..?z, 10 - ?a}]
|
||||
defp do_parse(<< char, bin :: binary >>) when char in ?0..?9, do: do_parse(bin, char - ?0)
|
||||
defp do_parse(_), do: :error
|
||||
|
||||
for {chars, diff} <- digits,
|
||||
char <- chars do
|
||||
digit = char + diff
|
||||
|
||||
defp count_digits_nosign(<<unquote(char), rest::bits>>, base, count)
|
||||
when base > unquote(digit) do
|
||||
count_digits_nosign(rest, base, count + 1)
|
||||
end
|
||||
defp do_parse(<< char, rest :: binary >>, acc) when char in ?0..?9 do
|
||||
do_parse rest, 10 * acc + (char - ?0)
|
||||
end
|
||||
|
||||
defp count_digits_nosign(<<_::bits>>, _, count), do: count
|
||||
defp do_parse(bitstring, acc) do
|
||||
{acc, bitstring}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36. If no `base` is given,
|
||||
it defaults to `10`.
|
||||
of `some_integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -326,164 +83,59 @@ defmodule Integer do
|
||||
iex> Integer.to_string(123)
|
||||
"123"
|
||||
|
||||
iex> Integer.to_string(+456)
|
||||
"456"
|
||||
|
||||
iex> Integer.to_string(-789)
|
||||
"-789"
|
||||
|
||||
iex> Integer.to_string(0123)
|
||||
"123"
|
||||
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
iex> Integer.to_string(-100, 16)
|
||||
"-64"
|
||||
|
||||
iex> Integer.to_string(882_681_651, 36)
|
||||
"ELIXIR"
|
||||
|
||||
"""
|
||||
@spec to_string(integer, 2..36) :: String.t()
|
||||
def to_string(integer, base \\ 10) do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
@spec to_string(integer) :: String.t
|
||||
def to_string(some_integer) do
|
||||
:erlang.integer_to_binary(some_integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36. If no `base` is given,
|
||||
it defaults to `10`.
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `some_integer` in base `base`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(123)
|
||||
~c"123"
|
||||
|
||||
iex> Integer.to_charlist(+456)
|
||||
~c"456"
|
||||
|
||||
iex> Integer.to_charlist(-789)
|
||||
~c"-789"
|
||||
|
||||
iex> Integer.to_charlist(0123)
|
||||
~c"123"
|
||||
|
||||
iex> Integer.to_charlist(100, 16)
|
||||
~c"64"
|
||||
|
||||
iex> Integer.to_charlist(-100, 16)
|
||||
~c"-64"
|
||||
|
||||
iex> Integer.to_charlist(882_681_651, 36)
|
||||
~c"ELIXIR"
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer, 2..36) :: charlist
|
||||
def to_charlist(integer, base \\ 10) do
|
||||
:erlang.integer_to_list(integer, base)
|
||||
@spec to_string(integer, 2..36) :: String.t
|
||||
def to_string(some_integer, base) do
|
||||
:erlang.integer_to_binary(some_integer, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the greatest common divisor of the two given integers.
|
||||
Returns a char list which corresponds to the text representation of the given integer.
|
||||
|
||||
The greatest common divisor (GCD) of `integer1` and `integer2` is the largest positive
|
||||
integer that divides both `integer1` and `integer2` without leaving a remainder.
|
||||
|
||||
By convention, `gcd(0, 0)` returns `0`.
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.gcd(2, 3)
|
||||
1
|
||||
|
||||
iex> Integer.gcd(8, 12)
|
||||
4
|
||||
|
||||
iex> Integer.gcd(8, -12)
|
||||
4
|
||||
|
||||
iex> Integer.gcd(10, 0)
|
||||
10
|
||||
|
||||
iex> Integer.gcd(7, 7)
|
||||
7
|
||||
|
||||
iex> Integer.gcd(0, 0)
|
||||
0
|
||||
iex> Integer.to_char_list(7)
|
||||
'7'
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec gcd(integer, integer) :: non_neg_integer
|
||||
def gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
|
||||
gcd_positive(abs(integer1), abs(integer2))
|
||||
@spec to_char_list(integer) :: char_list
|
||||
def to_char_list(number) do
|
||||
:erlang.integer_to_list(number)
|
||||
end
|
||||
|
||||
defp gcd_positive(0, integer2), do: integer2
|
||||
defp gcd_positive(integer1, 0), do: integer1
|
||||
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
|
||||
|
||||
@doc """
|
||||
Returns the extended greatest common divisor of the two given integers.
|
||||
Returns a char list which corresponds to the text representation of the
|
||||
given integer in the given case.
|
||||
|
||||
This function uses the extended Euclidean algorithm to return a three-element tuple with the `gcd`
|
||||
and the coefficients `m` and `n` of Bézout's identity such that:
|
||||
|
||||
gcd(a, b) = m*a + n*b
|
||||
|
||||
By convention, `extended_gcd(0, 0)` returns `{0, 0, 0}`.
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.extended_gcd(240, 46)
|
||||
{2, -9, 47}
|
||||
iex> Integer.extended_gcd(46, 240)
|
||||
{2, 47, -9}
|
||||
iex> Integer.extended_gcd(-46, 240)
|
||||
{2, -47, -9}
|
||||
iex> Integer.extended_gcd(-46, -240)
|
||||
{2, -47, 9}
|
||||
|
||||
iex> Integer.extended_gcd(14, 21)
|
||||
{7, -1, 1}
|
||||
|
||||
iex> Integer.extended_gcd(10, 0)
|
||||
{10, 1, 0}
|
||||
iex> Integer.extended_gcd(0, 10)
|
||||
{10, 0, 1}
|
||||
iex> Integer.extended_gcd(0, 0)
|
||||
{0, 0, 0}
|
||||
iex> Integer.to_char_list(1023, 16)
|
||||
'3FF'
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec extended_gcd(integer, integer) :: {non_neg_integer, integer, integer}
|
||||
def extended_gcd(0, 0), do: {0, 0, 0}
|
||||
def extended_gcd(0, b), do: {b, 0, 1}
|
||||
def extended_gcd(a, 0), do: {a, 1, 0}
|
||||
|
||||
def extended_gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
|
||||
extended_gcd(integer2, integer1, 0, 1, 1, 0)
|
||||
@spec to_char_list(integer, 2..36) :: char_list
|
||||
def to_char_list(number, base) do
|
||||
:erlang.integer_to_list(number, base)
|
||||
end
|
||||
|
||||
defp extended_gcd(r1, r0, s1, s0, t1, t0) do
|
||||
div = div(r0, r1)
|
||||
|
||||
case r0 - div * r1 do
|
||||
0 when r1 > 0 -> {r1, s1, t1}
|
||||
0 when r1 < 0 -> {-r1, -s1, -t1}
|
||||
r2 -> extended_gcd(r2, r1, s0 - div * s1, s1, t0 - div * t1, t1)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/1 instead"
|
||||
def to_char_list(integer), do: Integer.to_charlist(integer)
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/2 instead"
|
||||
def to_char_list(integer, base), do: Integer.to_charlist(integer, base)
|
||||
end
|
||||
|
||||
+250
-618
File diff suppressed because it is too large
Load Diff
+83
-213
@@ -1,9 +1,8 @@
|
||||
defmodule IO.ANSI.Sequence do
|
||||
@moduledoc false
|
||||
|
||||
defmacro defsequence(name, code, terminator \\ "m") do
|
||||
defmacro defsequence(name, code \\ "", terminator \\ "m") do
|
||||
quote bind_quoted: [name: name, code: code, terminator: terminator] do
|
||||
@spec unquote(name)() :: String.t()
|
||||
def unquote(name)() do
|
||||
"\e[#{unquote(code)}#{unquote(terminator)}"
|
||||
end
|
||||
@@ -17,51 +16,24 @@ end
|
||||
|
||||
defmodule IO.ANSI do
|
||||
@moduledoc """
|
||||
Functionality to render ANSI escape sequences.
|
||||
|
||||
[ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
||||
are characters embedded in text used to control formatting, color, and
|
||||
other output options on video text terminals.
|
||||
|
||||
ANSI escapes are typically enabled on all Unix terminals. They are also
|
||||
available on Windows consoles from Windows 10, although it must be
|
||||
explicitly enabled for the current user in the registry by running the
|
||||
following command:
|
||||
|
||||
reg add HKCU\\Console /v VirtualTerminalLevel /t REG_DWORD /d 1
|
||||
|
||||
After running the command above, you must restart your current console.
|
||||
|
||||
## Examples
|
||||
|
||||
Because the ANSI escape sequences are embedded in text, the normal usage of
|
||||
these functions is to concatenate their output with text.
|
||||
|
||||
formatted_text = IO.ANSI.blue_background() <> "Example" <> IO.ANSI.reset()
|
||||
IO.puts(formatted_text)
|
||||
|
||||
A higher level and more convenient API is also available via `IO.ANSI.format/1`,
|
||||
where you use atoms to represent each ANSI escape sequence and by default
|
||||
checks if ANSI is enabled:
|
||||
|
||||
IO.puts(IO.ANSI.format([:blue_background, "Example"]))
|
||||
|
||||
In case ANSI is disabled, the ANSI escape sequences are simply discarded.
|
||||
Functionality to render ANSI escape sequences
|
||||
(http://en.wikipedia.org/wiki/ANSI_escape_code) — characters embedded
|
||||
in text used to control formatting, color, and other output options
|
||||
on video text terminals.
|
||||
"""
|
||||
|
||||
import IO.ANSI.Sequence
|
||||
|
||||
@type ansicode :: atom
|
||||
@type ansilist ::
|
||||
maybe_improper_list(char | ansicode | binary | ansilist, binary | ansicode | [])
|
||||
@type ansidata :: ansilist | ansicode | binary
|
||||
@typep ansicode :: atom()
|
||||
@typep ansilist :: maybe_improper_list(char() | ansicode() | binary() | ansilist(), binary() | ansicode() | [])
|
||||
@type ansidata :: ansilist() | ansicode() | binary()
|
||||
|
||||
@doc """
|
||||
Checks if ANSI coloring is supported and enabled on this machine.
|
||||
|
||||
This function simply reads the configuration value for
|
||||
`:ansi_enabled` in the `:elixir` application. The value is by
|
||||
default `false` unless Elixir can detect during startup that
|
||||
default false unless Elixir can detect during startup that
|
||||
both `stdout` and `stderr` are terminals.
|
||||
"""
|
||||
@spec enabled? :: boolean
|
||||
@@ -69,196 +41,99 @@ defmodule IO.ANSI do
|
||||
Application.get_env(:elixir, :ansi_enabled, false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Syntax colors to be used by `Inspect`.
|
||||
@doc "Resets all attributes"
|
||||
defsequence :reset, 0
|
||||
|
||||
Those colors are used throughout Elixir's standard library,
|
||||
such as `dbg/2` and `IEx`.
|
||||
@doc "Bright (increased intensity) or Bold"
|
||||
defsequence :bright, 1
|
||||
|
||||
The colors can be changed by setting the `:ansi_syntax_colors`
|
||||
in the `:elixir` application configuration. Configuration for
|
||||
most built-in data types are supported: `:atom`, `:binary`,
|
||||
`:boolean`, `:charlist`, `:list`, `:map`, `:nil`, `:number`,
|
||||
`:string`, and `:tuple`. The default is:
|
||||
|
||||
[
|
||||
atom: :cyan
|
||||
boolean: :magenta,
|
||||
charlist: :yellow,
|
||||
nil: :magenta,
|
||||
number: :yellow,
|
||||
string: :green
|
||||
]
|
||||
|
||||
"""
|
||||
@doc since: "1.14.0"
|
||||
@spec syntax_colors :: Keyword.t(ansidata)
|
||||
def syntax_colors do
|
||||
Application.fetch_env!(:elixir, :ansi_syntax_colors)
|
||||
end
|
||||
|
||||
@doc "Sets foreground color."
|
||||
@spec color(0..255) :: String.t()
|
||||
def color(code) when code in 0..255, do: "\e[38;5;#{code}m"
|
||||
|
||||
@doc ~S"""
|
||||
Sets the foreground color from individual RGB values.
|
||||
|
||||
Valid values for each color are in the range 0 to 5.
|
||||
"""
|
||||
@spec color(0..5, 0..5, 0..5) :: String.t()
|
||||
def color(r, g, b) when r in 0..5 and g in 0..5 and b in 0..5 do
|
||||
color(16 + 36 * r + 6 * g + b)
|
||||
end
|
||||
|
||||
@doc "Sets background color."
|
||||
@spec color_background(0..255) :: String.t()
|
||||
def color_background(code) when code in 0..255, do: "\e[48;5;#{code}m"
|
||||
|
||||
@doc ~S"""
|
||||
Sets the background color from individual RGB values.
|
||||
|
||||
Valid values for each color are in the range 0 to 5.
|
||||
"""
|
||||
@spec color_background(0..5, 0..5, 0..5) :: String.t()
|
||||
def color_background(r, g, b) when r in 0..5 and g in 0..5 and b in 0..5 do
|
||||
color_background(16 + 36 * r + 6 * g + b)
|
||||
end
|
||||
|
||||
@doc "Resets all attributes."
|
||||
defsequence(:reset, 0)
|
||||
|
||||
@doc "Bright (increased intensity) or bold."
|
||||
defsequence(:bright, 1)
|
||||
|
||||
@doc "Faint (decreased intensity). Not widely supported."
|
||||
defsequence(:faint, 2)
|
||||
@doc "Faint (decreased intensity), not widely supported"
|
||||
defsequence :faint, 2
|
||||
|
||||
@doc "Italic: on. Not widely supported. Sometimes treated as inverse."
|
||||
defsequence(:italic, 3)
|
||||
defsequence :italic, 3
|
||||
|
||||
@doc "Underline: single."
|
||||
defsequence(:underline, 4)
|
||||
@doc "Underline: Single"
|
||||
defsequence :underline, 4
|
||||
|
||||
@doc "Blink: slow. Less than 150 per minute."
|
||||
defsequence(:blink_slow, 5)
|
||||
@doc "Blink: Slow. Less than 150 per minute"
|
||||
defsequence :blink_slow, 5
|
||||
|
||||
@doc "Blink: rapid. MS-DOS ANSI.SYS; 150 per minute or more; not widely supported."
|
||||
defsequence(:blink_rapid, 6)
|
||||
@doc "Blink: Rapid. MS-DOS ANSI.SYS; 150 per minute or more; not widely supported"
|
||||
defsequence :blink_rapid, 6
|
||||
|
||||
@doc "Image: negative. Swap foreground and background."
|
||||
defsequence(:inverse, 7)
|
||||
@doc "Image: Negative. Swap foreground and background"
|
||||
defsequence :inverse, 7
|
||||
|
||||
@doc "Image: negative. Swap foreground and background."
|
||||
defsequence(:reverse, 7)
|
||||
@doc "Image: Negative. Swap foreground and background"
|
||||
defsequence :reverse, 7
|
||||
|
||||
@doc "Conceal. Not widely supported."
|
||||
defsequence(:conceal, 8)
|
||||
@doc "Conceal. Not widely supported"
|
||||
defsequence :conceal, 8
|
||||
|
||||
@doc "Crossed-out. Characters legible, but marked for deletion. Not widely supported."
|
||||
defsequence(:crossed_out, 9)
|
||||
defsequence :crossed_out, 9
|
||||
|
||||
@doc "Sets primary (default) font."
|
||||
defsequence(:primary_font, 10)
|
||||
@doc "Sets primary (default) font"
|
||||
defsequence :primary_font, 10
|
||||
|
||||
for font_n <- [1, 2, 3, 4, 5, 6, 7, 8, 9] do
|
||||
@doc "Sets alternative font #{font_n}."
|
||||
defsequence(:"font_#{font_n}", font_n + 10)
|
||||
@doc "Sets alternative font #{font_n}"
|
||||
defsequence :"font_#{font_n}", font_n + 10
|
||||
end
|
||||
|
||||
@doc "Normal color or intensity."
|
||||
defsequence(:normal, 22)
|
||||
@doc "Normal color or intensity"
|
||||
defsequence :normal, 22
|
||||
|
||||
@doc "Not italic."
|
||||
defsequence(:not_italic, 23)
|
||||
@doc "Not italic"
|
||||
defsequence :not_italic, 23
|
||||
|
||||
@doc "Underline: none."
|
||||
defsequence(:no_underline, 24)
|
||||
@doc "Underline: None"
|
||||
defsequence :no_underline, 24
|
||||
|
||||
@doc "Blink: off."
|
||||
defsequence(:blink_off, 25)
|
||||
|
||||
@doc "Image: positive. Normal foreground and background."
|
||||
defsequence(:inverse_off, 27)
|
||||
|
||||
@doc "Image: positive. Normal foreground and background."
|
||||
defsequence(:reverse_off, 27)
|
||||
@doc "Blink: off"
|
||||
defsequence :blink_off, 25
|
||||
|
||||
colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
|
||||
colors = Enum.zip(0..(length(colors)-1), colors)
|
||||
|
||||
for {color, code} <- Enum.with_index(colors) do
|
||||
@doc "Sets foreground color to #{color}."
|
||||
defsequence(color, code + 30)
|
||||
for {code, color} <- colors do
|
||||
@doc "Sets foreground color to #{color}"
|
||||
defsequence color, code + 30
|
||||
|
||||
@doc "Sets foreground color to light #{color}."
|
||||
defsequence(:"light_#{color}", code + 90)
|
||||
|
||||
@doc "Sets background color to #{color}."
|
||||
defsequence(:"#{color}_background", code + 40)
|
||||
|
||||
@doc "Sets background color to light #{color}."
|
||||
defsequence(:"light_#{color}_background", code + 100)
|
||||
@doc "Sets background color to #{color}"
|
||||
defsequence :"#{color}_background", code + 40
|
||||
end
|
||||
|
||||
@doc "Default text color."
|
||||
defsequence(:default_color, 39)
|
||||
@doc "Default text color"
|
||||
defsequence :default_color, 39
|
||||
|
||||
@doc "Default background color."
|
||||
defsequence(:default_background, 49)
|
||||
@doc "Default background color"
|
||||
defsequence :default_background, 49
|
||||
|
||||
@doc "Framed."
|
||||
defsequence(:framed, 51)
|
||||
@doc "Framed"
|
||||
defsequence :framed, 51
|
||||
|
||||
@doc "Encircled."
|
||||
defsequence(:encircled, 52)
|
||||
@doc "Encircled"
|
||||
defsequence :encircled, 52
|
||||
|
||||
@doc "Overlined."
|
||||
defsequence(:overlined, 53)
|
||||
@doc "Overlined"
|
||||
defsequence :overlined, 53
|
||||
|
||||
@doc "Not framed or encircled."
|
||||
defsequence(:not_framed_encircled, 54)
|
||||
@doc "Not framed or encircled"
|
||||
defsequence :not_framed_encircled, 54
|
||||
|
||||
@doc "Not overlined."
|
||||
defsequence(:not_overlined, 55)
|
||||
@doc "Not overlined"
|
||||
defsequence :not_overlined, 55
|
||||
|
||||
@doc "Sends cursor home."
|
||||
defsequence(:home, "", "H")
|
||||
@doc "Send cursor home"
|
||||
defsequence :home, "", "H"
|
||||
|
||||
@doc """
|
||||
Sends cursor to the absolute position specified by `line` and `column`.
|
||||
|
||||
Line `0` and column `0` would mean the top left corner.
|
||||
"""
|
||||
@spec cursor(non_neg_integer, non_neg_integer) :: String.t()
|
||||
def cursor(line, column)
|
||||
when is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
|
||||
"\e[#{line};#{column}H"
|
||||
end
|
||||
|
||||
@doc "Sends cursor `lines` up."
|
||||
@spec cursor_up(pos_integer) :: String.t()
|
||||
def cursor_up(lines \\ 1) when is_integer(lines) and lines >= 1, do: "\e[#{lines}A"
|
||||
|
||||
@doc "Sends cursor `lines` down."
|
||||
@spec cursor_down(pos_integer) :: String.t()
|
||||
def cursor_down(lines \\ 1) when is_integer(lines) and lines >= 1, do: "\e[#{lines}B"
|
||||
|
||||
@doc "Sends cursor `columns` to the right."
|
||||
@spec cursor_right(pos_integer) :: String.t()
|
||||
def cursor_right(columns \\ 1) when is_integer(columns) and columns >= 1, do: "\e[#{columns}C"
|
||||
|
||||
@doc "Sends cursor `columns` to the left."
|
||||
@spec cursor_left(pos_integer) :: String.t()
|
||||
def cursor_left(columns \\ 1) when is_integer(columns) and columns >= 1, do: "\e[#{columns}D"
|
||||
|
||||
@doc "Clears screen."
|
||||
defsequence(:clear, "2", "J")
|
||||
|
||||
@doc "Clears line."
|
||||
defsequence(:clear_line, "2", "K")
|
||||
@doc "Clear screen"
|
||||
defsequence :clear, "2", "J"
|
||||
|
||||
defp format_sequence(other) do
|
||||
raise ArgumentError, "invalid ANSI sequence specification: #{inspect(other)}"
|
||||
raise ArgumentError, "invalid ANSI sequence specification: #{other}"
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -267,24 +142,21 @@ defmodule IO.ANSI do
|
||||
|
||||
The named sequences are represented by atoms.
|
||||
|
||||
It will also append an `IO.ANSI.reset/0` to the chardata when a conversion is
|
||||
performed. If you don't want this behavior, use `format_fragment/2`.
|
||||
It will also append an `IO.ANSI.reset` to the chardata when a conversion is
|
||||
performed. If you don't want this behaviour, use `format_fragment/2`.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
An `ArgumentError` will be raised if an invalid ANSI code is provided.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.ANSI.format(["Hello, ", :red, :bright, "world!"], true)
|
||||
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
|
||||
|
||||
"""
|
||||
@spec format(ansidata, boolean) :: IO.chardata()
|
||||
def format(ansidata, emit? \\ enabled?()) when is_boolean(emit?) do
|
||||
do_format(ansidata, [], [], emit?, :maybe)
|
||||
def format(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, :maybe)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -294,22 +166,21 @@ defmodule IO.ANSI do
|
||||
The named sequences are represented by atoms.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.ANSI.format_fragment([:bright, ~c"Word"], true)
|
||||
iex> IO.ANSI.format_fragment([:bright, 'Word'], true)
|
||||
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
|
||||
|
||||
"""
|
||||
@spec format_fragment(ansidata, boolean) :: IO.chardata()
|
||||
def format_fragment(ansidata, emit? \\ enabled?()) when is_boolean(emit?) do
|
||||
do_format(ansidata, [], [], emit?, false)
|
||||
def format_fragment(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, false)
|
||||
end
|
||||
|
||||
defp do_format([term | rest], rem, acc, emit?, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit?, append_reset)
|
||||
defp do_format([term | rest], rem, acc, emit, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, true, append_reset) when is_atom(term) do
|
||||
@@ -317,23 +188,22 @@ defmodule IO.ANSI do
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, false, append_reset) when is_atom(term) do
|
||||
format_sequence(term)
|
||||
do_format([], rem, acc, false, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, emit?, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc, term], emit?, append_reset)
|
||||
defp do_format(term, rem, acc, emit, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc | [term]], emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [next | rest], acc, emit?, append_reset) do
|
||||
do_format(next, rest, acc, emit?, append_reset)
|
||||
defp do_format([], [next | rest], acc, emit, append_reset) do
|
||||
do_format(next, rest, acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, true, true) do
|
||||
[acc | IO.ANSI.reset()]
|
||||
[acc | IO.ANSI.reset]
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, _emit?, _append_reset) do
|
||||
defp do_format([], [], acc, _emit, _append_reset) do
|
||||
acc
|
||||
end
|
||||
end
|
||||
|
||||
+208
-486
@@ -1,46 +1,36 @@
|
||||
defmodule IO.ANSI.Docs do
|
||||
@moduledoc false
|
||||
|
||||
@bullet_text_unicode "• "
|
||||
@bullet_text_ascii "* "
|
||||
@bullets [?*, ?-, ?+]
|
||||
@spaces [" ", "\n", "\t"]
|
||||
|
||||
@doc """
|
||||
The default options used by this module.
|
||||
|
||||
The supported keys are:
|
||||
The supported values are:
|
||||
|
||||
* `:enabled` - toggles coloring on and off (true)
|
||||
* `:doc_bold` - bold text (bright)
|
||||
* `:doc_code` - code blocks (cyan)
|
||||
* `:doc_headings` - h1, h2, h3, h4, h5, h6 headings (yellow)
|
||||
* `:doc_metadata` - documentation metadata keys (yellow)
|
||||
* `:doc_quote` - leading quote character `> ` (light black)
|
||||
* `:doc_code` - code blocks (cyan, bright)
|
||||
* `:doc_headings` - h1 and h2 headings (yellow, bright)
|
||||
* `:doc_inline_code` - inline code (cyan)
|
||||
* `:doc_table_heading` - the style for table headings
|
||||
* `:doc_title` - top level heading (reverse, yellow)
|
||||
* `:doc_table_heading` - style for table headings
|
||||
* `:doc_title` - top level heading (reverse, yellow, bright)
|
||||
* `:doc_underline` - underlined text (underline)
|
||||
* `:width` - the width to format the text (80)
|
||||
|
||||
Values for the color settings are strings with
|
||||
comma-separated ANSI values.
|
||||
"""
|
||||
@spec default_options() :: keyword
|
||||
def default_options do
|
||||
[
|
||||
enabled: true,
|
||||
doc_bold: [:bright],
|
||||
doc_code: [:cyan],
|
||||
doc_headings: [:yellow],
|
||||
doc_metadata: [:yellow],
|
||||
doc_quote: [:light_black],
|
||||
doc_inline_code: [:cyan],
|
||||
doc_table_heading: [:reverse],
|
||||
doc_title: [:reverse, :yellow],
|
||||
doc_underline: [:underline],
|
||||
width: 80
|
||||
]
|
||||
[enabled: true,
|
||||
doc_bold: [:bright],
|
||||
doc_code: [:cyan, :bright],
|
||||
doc_headings: [:yellow],
|
||||
doc_inline_code: [:cyan],
|
||||
doc_table_heading: [:reverse],
|
||||
doc_title: [:reverse, :yellow],
|
||||
doc_underline: [:underline],
|
||||
width: 80]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -48,88 +38,27 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
See `default_options/0` for docs on the supported options.
|
||||
"""
|
||||
@spec print_headings([String.t()], keyword) :: :ok
|
||||
def print_headings(headings, options \\ []) do
|
||||
# It's possible for some of the headings to contain newline characters (`\n`), so in order to prevent it from
|
||||
# breaking the output from `print_headings/2`, as `print_headings/2` tries to pad the whole heading, we first split
|
||||
# any heading containgin newline characters into multiple headings, that way each one is padded on its own.
|
||||
headings = Enum.flat_map(headings, fn heading -> String.split(heading, "\n") end)
|
||||
options = Keyword.merge(default_options(), options)
|
||||
newline_after_block(options)
|
||||
width = options[:width]
|
||||
|
||||
for heading <- headings do
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = String.pad_leading(heading, padding)
|
||||
heading = if options[:enabled], do: String.pad_trailing(heading, width), else: heading
|
||||
write(:doc_title, heading, options)
|
||||
end
|
||||
|
||||
newline_after_block(options)
|
||||
def print_heading(heading, options \\ []) do
|
||||
IO.puts IO.ANSI.reset
|
||||
options = Keyword.merge(default_options, options)
|
||||
width = options[:width]
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = heading |> String.rjust(padding) |> String.ljust(width)
|
||||
write(:doc_title, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints documentation metadata (only `delegate_to`, `deprecated`, `guard`, and `since` for now).
|
||||
Prints the documentation body.
|
||||
|
||||
See `default_options/0` for docs on the supported options.
|
||||
In addition to the printing string, takes a set of options
|
||||
defined in `default_options/1`.
|
||||
"""
|
||||
@spec print_metadata(map, keyword) :: :ok
|
||||
def print_metadata(metadata, options \\ []) when is_map(metadata) do
|
||||
options = Keyword.merge(default_options(), options)
|
||||
print_each_metadata(metadata, options) && IO.write("\n")
|
||||
end
|
||||
|
||||
@metadata_filter [:deprecated, :guard, :since]
|
||||
|
||||
defp print_each_metadata(metadata, options) do
|
||||
metadata
|
||||
|> Enum.sort()
|
||||
|> Enum.reduce(false, fn
|
||||
{key, value}, _printed when is_binary(value) and key in @metadata_filter ->
|
||||
label = metadata_label(key, options)
|
||||
indent = String.duplicate(" ", length_without_escape(label, 0) + 1)
|
||||
write_with_wrap([label | String.split(value, @spaces)], options[:width], indent, true, "")
|
||||
|
||||
{key, value}, _printed when is_boolean(value) and key in @metadata_filter ->
|
||||
IO.puts([metadata_label(key, options), ?\s, to_string(value)])
|
||||
|
||||
{:delegate_to, {m, f, a}}, _printed ->
|
||||
label = metadata_label(:delegate_to, options)
|
||||
IO.puts([label, ?\s, Exception.format_mfa(m, f, a)])
|
||||
|
||||
_metadata, printed ->
|
||||
printed
|
||||
end)
|
||||
end
|
||||
|
||||
defp metadata_label(key, options) do
|
||||
"#{color(:doc_metadata, options)}#{key}:#{maybe_reset(options)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints the documentation body `doc` according to `format`.
|
||||
|
||||
It takes a set of `options` defined in `default_options/0`.
|
||||
"""
|
||||
@spec print(term(), String.t(), keyword) :: :ok
|
||||
def print(doc, format, options \\ [])
|
||||
|
||||
def print(doc, "text/markdown", options) when is_binary(doc) and is_list(options) do
|
||||
print_markdown(doc, options)
|
||||
end
|
||||
|
||||
def print(_doc, format, options) when is_binary(format) and is_list(options) do
|
||||
IO.puts("\nUnknown documentation format #{inspect(format)}\n")
|
||||
end
|
||||
|
||||
## Markdown
|
||||
|
||||
def print_markdown(doc, options) do
|
||||
options = Keyword.merge(default_options(), options)
|
||||
|
||||
def print(doc, options \\ []) do
|
||||
options = Keyword.merge(default_options, options)
|
||||
doc
|
||||
|> String.split(["\r\n", "\n"], trim: false)
|
||||
|> Enum.map(&String.trim_trailing/1)
|
||||
|> String.split(["\r\n","\n"], trim: false)
|
||||
|> Enum.map(&String.rstrip/1)
|
||||
|> process([], "", options)
|
||||
end
|
||||
|
||||
@@ -137,33 +66,22 @@ defmodule IO.ANSI.Docs do
|
||||
write_text(text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["# " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["## " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["#### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["##### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["###### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process([">" <> line | rest], text, indent, options) do
|
||||
defp process(["# " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
process_quote(rest, [line], indent, options)
|
||||
write_h1(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["## " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h2(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["### " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h3(String.strip(heading), indent, options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["" | rest], text, indent, options) do
|
||||
@@ -176,122 +94,61 @@ defmodule IO.ANSI.Docs do
|
||||
process_code(rest, [line], indent, options)
|
||||
end
|
||||
|
||||
defp process(["```mermaid" <> _line | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
|
||||
rest
|
||||
|> Enum.drop_while(&(&1 != "```"))
|
||||
|> Enum.drop(1)
|
||||
|> process([], indent, options)
|
||||
end
|
||||
|
||||
defp process(["```" <> _line | rest], text, indent, options) do
|
||||
process_fenced_code_block(rest, text, indent, options, _delimiter = "```")
|
||||
end
|
||||
|
||||
defp process(["<!--" <> line | rest], text, indent, options) do
|
||||
process(drop_comment([line | rest]), text, indent, options)
|
||||
end
|
||||
|
||||
defp process(all = [line | rest], text, indent, options) do
|
||||
defp process(all=[line | rest], text, indent, options) do
|
||||
{stripped, count} = strip_spaces(line, 0, :infinity)
|
||||
|
||||
cond do
|
||||
link_label?(stripped, count) ->
|
||||
write_text([line], indent, options, true)
|
||||
process(rest, text, indent, options)
|
||||
|
||||
table_line?(stripped) and rest != [] and table_line?(hd(rest)) ->
|
||||
write_text(text, indent, options)
|
||||
process_table(all, indent, options)
|
||||
|
||||
true ->
|
||||
process_rest(stripped, rest, count, text, indent, options)
|
||||
if is_table_line?(stripped) and rest != [] and is_table_line?(hd(rest)) do
|
||||
write_text(text, indent, options)
|
||||
process_table(all, indent, options)
|
||||
else
|
||||
case stripped do
|
||||
<<bullet, ?\s, item :: binary>> when bullet in @bullets ->
|
||||
write_text(text, indent, options)
|
||||
process_list("• ", item, rest, count, indent, options)
|
||||
<<d1, ?., ?\s, item :: binary>> when d1 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, ?., ?\s>>, item, rest, count, indent, options)
|
||||
<<d1, d2, ?., ?\s, item :: binary>> when d1 in ?0..?9 and d2 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, d2, ?., ?\s>>, item, rest, count, indent, options)
|
||||
_ ->
|
||||
process(rest, [stripped | text], indent, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
### Headings
|
||||
## Headings
|
||||
|
||||
defp write_heading(heading, rest, text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
defp write_h1(heading, options) do
|
||||
write_h2(String.upcase(heading), options)
|
||||
end
|
||||
|
||||
defp write_h2(heading, options) do
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block(options)
|
||||
process(rest, [], "", options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
### Quotes
|
||||
|
||||
defp process_quote([">", ">" <> line | rest], lines, indent, options) do
|
||||
write_quote(lines, indent, options, true)
|
||||
write_empty_quote_line(options)
|
||||
process_quote(rest, [line], indent, options)
|
||||
defp write_h3(heading, indent, options) do
|
||||
IO.write(indent)
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
defp process_quote([">" <> line | rest], lines, indent, options) do
|
||||
process_quote(rest, [line | lines], indent, options)
|
||||
end
|
||||
|
||||
defp process_quote(rest, lines, indent, options) do
|
||||
write_quote(lines, indent, options, false)
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp write_quote(lines, indent, options, no_wrap) do
|
||||
lines
|
||||
|> Enum.map(&String.trim/1)
|
||||
|> Enum.reverse()
|
||||
|> write_lines(
|
||||
indent,
|
||||
options,
|
||||
no_wrap,
|
||||
quote_prefix(options)
|
||||
)
|
||||
end
|
||||
|
||||
defp write_empty_quote_line(options) do
|
||||
options
|
||||
|> quote_prefix()
|
||||
|> IO.puts()
|
||||
end
|
||||
|
||||
### Lists
|
||||
|
||||
defp process_rest(stripped, rest, count, text, indent, options) do
|
||||
case stripped do
|
||||
<<bullet, ?\s, item::binary>> when bullet in @bullets ->
|
||||
write_text(text, indent, options)
|
||||
process_list(bullet_text(options), item, rest, count, indent, options)
|
||||
|
||||
<<d1, ?., ?\s, item::binary>> when d1 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, ?., ?\s>>, item, rest, count, indent, options)
|
||||
|
||||
<<d1, d2, ?., ?\s, item::binary>> when d1 in ?0..?9 and d2 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, d2, ?., ?\s>>, item, rest, count, indent, options)
|
||||
|
||||
_ ->
|
||||
process(rest, [stripped | text], indent, options)
|
||||
end
|
||||
end
|
||||
## Lists
|
||||
|
||||
defp process_list(entry, line, rest, count, indent, options) do
|
||||
# The first list always win some extra padding
|
||||
entry = if indent == "", do: " " <> entry, else: entry
|
||||
if indent == "", do: entry = " " <> entry
|
||||
new_indent = indent <> String.duplicate(" ", String.length(entry))
|
||||
|
||||
{contents, rest, done} =
|
||||
process_list_next(rest, count, byte_size(new_indent) - byte_size(indent), [])
|
||||
|
||||
{contents, rest, done} = process_list_next(rest, count, byte_size(new_indent), [])
|
||||
process(contents, [indent <> entry <> line, :no_wrap], new_indent, options)
|
||||
|
||||
if done, do: newline_after_block(options)
|
||||
if done, do: newline_after_block()
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp process_list_next([line | rest], count, max, acc) do
|
||||
{stripped, next_count} = strip_spaces(line, 0, max)
|
||||
|
||||
case process_list_next_kind(stripped, rest, count, next_count) do
|
||||
:next -> process_list_next(rest, count, max, [stripped | acc])
|
||||
:done -> {Enum.reverse(acc), [line | rest], true}
|
||||
@@ -305,32 +162,26 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp process_list_next_kind(stripped, rest, count, next_count) do
|
||||
case {stripped, rest} do
|
||||
{<<bullet, ?\s, _::binary>>, _} when bullet in @bullets and next_count <= count ->
|
||||
{<<bullet, ?\s, _ :: binary>>, _} when bullet in @bullets and next_count <= count ->
|
||||
:list
|
||||
|
||||
{<<d1, ?., ?\s, _::binary>>, _} when d1 in ?0..?9 and next_count <= count ->
|
||||
{<<d1, ?., ?\s, _ :: binary>>, _} when d1 in ?0..?9 and next_count <= count ->
|
||||
:list
|
||||
|
||||
{<<d1, d2, ?., ?\s, _::binary>>, _}
|
||||
when d1 in ?0..?9 and d2 in ?0..?9 and next_count <= count ->
|
||||
{<<d1, d2, ?., ?\s, _ :: binary>>, _} when d1 in ?0..?9 and d2 in ?0..?9 and next_count <= count ->
|
||||
:list
|
||||
|
||||
{"", [" " <> _ | _]} ->
|
||||
:next
|
||||
|
||||
{"", _} ->
|
||||
:done
|
||||
|
||||
_ ->
|
||||
:next
|
||||
end
|
||||
end
|
||||
|
||||
### Text
|
||||
## Text
|
||||
|
||||
defp write_text(text, indent, options) do
|
||||
case Enum.reverse(text) do
|
||||
[:no_wrap | rest] -> write_text(rest, indent, options, true)
|
||||
[:no_wrap|rest] -> write_text(rest, indent, options, true)
|
||||
rest -> write_text(rest, indent, options, false)
|
||||
end
|
||||
end
|
||||
@@ -340,26 +191,21 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp write_text(lines, indent, options, no_wrap) do
|
||||
write_lines(lines, indent, options, no_wrap, "")
|
||||
end
|
||||
|
||||
defp write_lines(lines, indent, options, no_wrap, prefix) do
|
||||
lines
|
||||
|> Enum.join(" ")
|
||||
|> format_text(options)
|
||||
|> String.split(@spaces)
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap, prefix)
|
||||
|
||||
unless no_wrap, do: newline_after_block(options)
|
||||
end
|
||||
|
||||
defp format_text(text, options) do
|
||||
text
|
||||
|> handle_links()
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|> String.split(~r{\s})
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
||||
|
||||
unless no_wrap, do: newline_after_block()
|
||||
end
|
||||
|
||||
### Code blocks
|
||||
## Code blocks
|
||||
|
||||
defp process_code([], code, indent, options) do
|
||||
write_code(code, indent, options)
|
||||
end
|
||||
|
||||
# Blank line between code blocks
|
||||
defp process_code(["", " " <> line | rest], code, indent, options) do
|
||||
@@ -367,7 +213,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp process_code([" " <> line | rest], code, indent, options) do
|
||||
process_code(rest, [line | code], indent, options)
|
||||
process_code(rest, [line|code], indent, options)
|
||||
end
|
||||
|
||||
defp process_code(rest, code, indent, options) do
|
||||
@@ -375,71 +221,48 @@ defmodule IO.ANSI.Docs do
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp process_fenced_code_block(rest, text, indent, options, delimiter) do
|
||||
write_text(text, indent, options)
|
||||
process_fenced_code(rest, [], indent, options, delimiter)
|
||||
end
|
||||
|
||||
defp process_fenced_code([], code, indent, options, _delimiter) do
|
||||
write_code(code, indent, options)
|
||||
end
|
||||
|
||||
defp process_fenced_code([line | rest], code, indent, options, delimiter) do
|
||||
if line == delimiter do
|
||||
process_code(rest, code, indent, options)
|
||||
else
|
||||
process_fenced_code(rest, [line | code], indent, options, delimiter)
|
||||
end
|
||||
end
|
||||
|
||||
defp write_code(code, indent, options) do
|
||||
write(:doc_code, "#{indent} #{Enum.join(Enum.reverse(code), "\n#{indent} ")}", options)
|
||||
newline_after_block(options)
|
||||
write(:doc_code, "#{indent}┃ #{Enum.join(Enum.reverse(code), "\n#{indent}┃ ")}", options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
### Tables
|
||||
## Tables
|
||||
|
||||
defp process_table(lines, indent, options) do
|
||||
{table, rest} = Enum.split_while(lines, &table_line?/1)
|
||||
{table, rest} = Enum.split_while(lines, &is_table_line?/1)
|
||||
table_lines(table, options)
|
||||
newline_after_block(options)
|
||||
newline_after_block
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp table_lines(lines, options) do
|
||||
lines = Enum.map(lines, &split_into_columns(&1, options))
|
||||
count = Enum.map(lines, &length/1) |> Enum.max()
|
||||
count = Enum.map(lines, &length/1) |> Enum.max
|
||||
lines = Enum.map(lines, &pad_to_number_of_columns(&1, count))
|
||||
|
||||
widths =
|
||||
for line <- lines do
|
||||
if table_header?(line) do
|
||||
for _ <- line, do: 0
|
||||
else
|
||||
for {_col, length} <- line, do: length
|
||||
end
|
||||
end
|
||||
widths = for line <- lines, do:
|
||||
(for {_col, length} <- line, do: length)
|
||||
|
||||
col_widths = Enum.reduce(widths,
|
||||
List.duplicate(0, count),
|
||||
&max_column_widths/2)
|
||||
|
||||
col_widths = Enum.reduce(widths, List.duplicate(0, count), &max_column_widths/2)
|
||||
render_table(lines, col_widths, options)
|
||||
end
|
||||
|
||||
defp split_into_columns(line, options) do
|
||||
line
|
||||
|> String.trim(" ")
|
||||
|> String.trim("|")
|
||||
|> String.split(~r{(?<!\\)\|})
|
||||
|> String.strip(?|)
|
||||
|> String.strip()
|
||||
|> String.split(~r/\s\|\s/)
|
||||
|> Enum.map(&render_column(&1, options))
|
||||
end
|
||||
|
||||
defp render_column(col, options) do
|
||||
col =
|
||||
col
|
||||
|> String.trim()
|
||||
|> String.replace("\\\|", "|")
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|
||||
col = col
|
||||
|> String.replace(~r/\\ \|/x, "|")
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
{col, length_without_escape(col, 0)}
|
||||
end
|
||||
|
||||
@@ -447,15 +270,12 @@ defmodule IO.ANSI.Docs do
|
||||
do: cols ++ List.duplicate({"", 0}, col_count - length(cols))
|
||||
|
||||
defp max_column_widths(cols, widths),
|
||||
do: Enum.zip(cols, widths) |> Enum.map(fn {a, b} -> max(a, b) end)
|
||||
do: Enum.zip(cols, widths) |> Enum.map(fn {a,b} -> max(a,b) end)
|
||||
|
||||
# If second line is heading separator, use the heading style on the first
|
||||
defp render_table([first, second | rest], widths, options) do
|
||||
combined = Enum.zip(first, widths)
|
||||
|
||||
if table_header?(second) do
|
||||
alignments = Enum.map(second, &column_alignment/1)
|
||||
options = Keyword.put_new(options, :alignments, alignments)
|
||||
draw_table_row(combined, options, :heading)
|
||||
render_table(rest, widths, options)
|
||||
else
|
||||
@@ -470,114 +290,61 @@ defmodule IO.ANSI.Docs do
|
||||
render_table(rest, widths, options)
|
||||
end
|
||||
|
||||
defp render_table([], _, _), do: nil
|
||||
defp render_table([], _, _),
|
||||
do: nil
|
||||
|
||||
defp column_alignment({line, _}) do
|
||||
cond do
|
||||
String.starts_with?(line, ":") and String.ends_with?(line, ":") -> :center
|
||||
String.ends_with?(line, ":") -> :right
|
||||
true -> :left
|
||||
end
|
||||
end
|
||||
|
||||
defp table_header?(line) do
|
||||
Enum.all?(line, fn {col, _} -> table_header_column?(col) end)
|
||||
end
|
||||
|
||||
defp table_header_column?(":" <> rest), do: table_header_contents?(rest)
|
||||
defp table_header_column?(col), do: table_header_contents?(col)
|
||||
|
||||
defp table_header_contents?("-" <> rest), do: table_header_contents?(rest)
|
||||
defp table_header_contents?(":"), do: true
|
||||
defp table_header_contents?(""), do: true
|
||||
defp table_header_contents?(_), do: false
|
||||
defp table_header?(row), do:
|
||||
Enum.all?(row, fn {col, _} -> col =~ ~r/^:?-+:?$/ end)
|
||||
|
||||
defp draw_table_row(cols_and_widths, options, heading \\ false) do
|
||||
default_alignments = List.duplicate(:left, length(cols_and_widths))
|
||||
alignments = Keyword.get(options, :alignments, default_alignments)
|
||||
|
||||
columns =
|
||||
cols_and_widths
|
||||
|> Enum.zip(alignments)
|
||||
|> Enum.map_join(" | ", &generate_table_cell/1)
|
||||
Enum.map_join(cols_and_widths, " | ", fn {{col, length}, width} ->
|
||||
col <> String.duplicate(" ", width - length)
|
||||
end)
|
||||
|
||||
if heading do
|
||||
write(:doc_table_heading, columns, options)
|
||||
else
|
||||
IO.puts(columns)
|
||||
IO.puts columns
|
||||
end
|
||||
end
|
||||
|
||||
defp generate_table_cell({{{col, length}, width}, :center}) do
|
||||
ansi_diff = byte_size(col) - length
|
||||
width = width + ansi_diff
|
||||
|
||||
col
|
||||
|> String.pad_leading(div(width, 2) - div(length, 2) + length)
|
||||
|> String.pad_trailing(width + 1 - rem(width, 2))
|
||||
end
|
||||
|
||||
defp generate_table_cell({{{col, length}, width}, :right}) do
|
||||
ansi_diff = byte_size(col) - length
|
||||
String.pad_leading(col, width + ansi_diff)
|
||||
end
|
||||
|
||||
defp generate_table_cell({{{col, length}, width}, :left}) do
|
||||
ansi_diff = byte_size(col) - length
|
||||
String.pad_trailing(col, width + ansi_diff)
|
||||
end
|
||||
|
||||
defp table_line?(line) do
|
||||
line =~ ~r/[:\ -]\|[:\ -]/
|
||||
defp is_table_line?(line) do
|
||||
Regex.match?(~r'''
|
||||
( ^ \s{0,3} \| (?: [^|]+ \|)+ \s* $ )
|
||||
|
|
||||
(\s \| \s)
|
||||
'''x, line)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp link_label?("[" <> rest, count) when count <= 3, do: link_label?(rest)
|
||||
defp link_label?(_, _), do: false
|
||||
|
||||
defp link_label?("]: " <> _), do: true
|
||||
defp link_label?("]" <> _), do: false
|
||||
defp link_label?(""), do: false
|
||||
defp link_label?(<<_>> <> rest), do: link_label?(rest)
|
||||
|
||||
defp strip_spaces(" " <> line, acc, max) when acc < max, do: strip_spaces(line, acc + 1, max)
|
||||
defp strip_spaces(rest, acc, _max), do: {rest, acc}
|
||||
defp strip_spaces(" " <> line, acc, max) when acc < max,
|
||||
do: strip_spaces(line, acc + 1, max)
|
||||
defp strip_spaces(rest, acc, _max),
|
||||
do: {rest, acc}
|
||||
|
||||
defp write(style, string, options) do
|
||||
IO.puts([color(style, options), string, maybe_reset(options)])
|
||||
IO.puts [color(style, options), string, IO.ANSI.reset]
|
||||
end
|
||||
|
||||
defp write_with_wrap([], _available, _indent, _first, _prefix) do
|
||||
defp write_with_wrap([], _available, _indent, _first) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp write_with_wrap(words, available, indent, first, prefix) do
|
||||
words
|
||||
|> wrap_text(available, indent, first, prefix, [])
|
||||
|> tl()
|
||||
|> IO.puts()
|
||||
defp write_with_wrap(words, available, indent, first) do
|
||||
{words, rest} = take_words(words, available, [])
|
||||
IO.puts (if first, do: "", else: indent) <> Enum.join(words, " ")
|
||||
write_with_wrap(rest, available, indent, false)
|
||||
end
|
||||
|
||||
defp wrap_text([], _available, _indent, _first, _prefix, wrapped_lines) do
|
||||
Enum.reverse(wrapped_lines)
|
||||
end
|
||||
|
||||
defp wrap_text(words, available, indent, first, prefix, wrapped_lines) do
|
||||
prefix_length = length_without_escape(prefix, 0)
|
||||
{words, rest} = take_words(words, available - prefix_length, [])
|
||||
line = [if(first, do: "", else: indent), prefix, Enum.join(words, " ")]
|
||||
|
||||
wrap_text(rest, available, indent, false, prefix, [line, ?\n | wrapped_lines])
|
||||
end
|
||||
|
||||
defp take_words([word | words], available, acc) do
|
||||
defp take_words([word|words], available, acc) do
|
||||
available = available - length_without_escape(word, 0)
|
||||
|
||||
cond do
|
||||
# It fits, take one for space and continue decreasing
|
||||
available > 0 ->
|
||||
take_words(words, available - 1, [word | acc])
|
||||
take_words(words, available - 1, [word|acc])
|
||||
|
||||
# No space but we got no words
|
||||
acc == [] ->
|
||||
@@ -585,7 +352,7 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
# Otherwise
|
||||
true ->
|
||||
{Enum.reverse(acc), [word | words]}
|
||||
{Enum.reverse(acc), [word|words]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -593,11 +360,11 @@ defmodule IO.ANSI.Docs do
|
||||
{Enum.reverse(acc), []}
|
||||
end
|
||||
|
||||
defp length_without_escape(<<?\e, ?[, _, _, ?m>> <> rest, count) do
|
||||
defp length_without_escape(<< ?\e, ?[, _, _, ?m, rest :: binary >>, count) do
|
||||
length_without_escape(rest, count)
|
||||
end
|
||||
|
||||
defp length_without_escape(<<?\e, ?[, _, ?m>> <> rest, count) do
|
||||
defp length_without_escape(<< ?\e, ?[, _, ?m, rest :: binary >>, count) do
|
||||
length_without_escape(rest, count)
|
||||
end
|
||||
|
||||
@@ -615,38 +382,24 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp escape_underlines_in_link(text) do
|
||||
# Regular expression adapted from https://tools.ietf.org/html/rfc3986#appendix-B
|
||||
Regex.replace(~r{[a-z][a-z0-9\+\-\.]*://\S*}i, text, &String.replace(&1, "_", "\\_"))
|
||||
end
|
||||
|
||||
defp remove_square_brackets_in_link(text) do
|
||||
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
defp drop_comment(line) when is_binary(line) do
|
||||
[_comment, rest] = :binary.split(line, "-->")
|
||||
rest
|
||||
end
|
||||
|
||||
defp drop_comment([line | rest]) do
|
||||
case :binary.split(line, "-->") do
|
||||
[_] -> drop_comment(rest)
|
||||
[_, line] -> [line | rest]
|
||||
case Regex.match?(~r{.*(https?\S*)}, text) do
|
||||
true -> Regex.replace(~r{_}, text, "\\\\_")
|
||||
_ -> text
|
||||
end
|
||||
end
|
||||
|
||||
defp drop_comment([]) do
|
||||
[]
|
||||
defp remove_square_brackets_in_link(text) do
|
||||
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
# We have four entries: **, __, *, _ and `.
|
||||
|
||||
# We have four entries: **, *, _ and `.
|
||||
#
|
||||
# The first four behave the same while the last one is simpler
|
||||
# when it comes to delimiters as it ignores spaces and escape
|
||||
# characters. But, since the first two has two characters,
|
||||
# we need to handle 3 cases:
|
||||
# The first three behave the same while the last one is simpler
|
||||
# when it comes to delimiters. But, since the first has two
|
||||
# characters, we need to handle 3 cases:
|
||||
#
|
||||
# 1. __ and **
|
||||
# 1. **
|
||||
# 2. _ and *
|
||||
# 3. `
|
||||
#
|
||||
@@ -655,16 +408,15 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
# Characters that can mark the beginning or the end of a word.
|
||||
# Only support the most common ones at this moment.
|
||||
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&] ++
|
||||
[?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>, ?.]
|
||||
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&, ?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>]
|
||||
|
||||
### Inline start
|
||||
# Inline start
|
||||
|
||||
defp handle_inline(<<mark, mark, rest::binary>>, options) when mark in @single do
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], [], options)
|
||||
defp handle_inline(<<?*, ?*, rest :: binary>>, options) do
|
||||
handle_inline(rest, ?d, ["**"], [], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
|
||||
defp handle_inline(<<mark, rest :: binary>>, options) when mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [], options)
|
||||
end
|
||||
|
||||
@@ -672,129 +424,99 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(rest, nil, [], [], options)
|
||||
end
|
||||
|
||||
### Inline delimiters
|
||||
# Inline delimiters
|
||||
|
||||
defp handle_inline("<!--" <> rest, nil, buffer, acc, options) do
|
||||
rest = drop_comment(rest)
|
||||
handle_inline(rest, [], buffer, acc, options)
|
||||
defp handle_inline(<<delimiter, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters do
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<delimiter, mark, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
acc = [delimiter, Enum.reverse(buffer) | acc]
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
|
||||
defp handle_inline(<<delimiter, mark, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer) | acc], options)
|
||||
defp handle_inline(<<?`, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer) | acc], options)
|
||||
# Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
### Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
acc = [?\\, Enum.reverse(buffer) | acc]
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer) | acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, rest::binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?\\ | buffer], acc, options)
|
||||
defp handle_inline(<<?\\, ?\\, rest :: binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?\\|buffer], acc, options)
|
||||
end
|
||||
|
||||
# An escape is not valid inside `
|
||||
defp handle_inline(<<?\\, mark, rest::binary>>, limit, buffer, acc, options) when limit != ?` do
|
||||
handle_inline(rest, limit, [mark | buffer], acc, options)
|
||||
defp handle_inline(<<?\\, mark, rest :: binary>>, limit, buffer, acc, options)
|
||||
when not(mark == limit and mark == ?`) do
|
||||
handle_inline(rest, limit, [mark|buffer], acc, options)
|
||||
end
|
||||
|
||||
### Inline end
|
||||
# Inline end
|
||||
|
||||
defp handle_inline(<<mark, mark, delimiter, rest::binary>>, [mark | mark], buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
inline_buffer = inline_buffer(buffer, options)
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest :: binary>>, ?d, buffer, acc, options)
|
||||
when delimiter in @delimiters do
|
||||
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, delimiter, rest::binary>>, mark, buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
inline_buffer = inline_buffer(buffer, options)
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
|
||||
defp handle_inline(<<mark, delimiter, rest :: binary>>, mark, buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, mark, rest::binary>>, [mark | mark], buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
defp handle_inline(<<?*, ?*, rest:: binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, mark, buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
defp handle_inline(<<mark, rest :: binary>>, mark, buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, ?`, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
defp handle_inline(<<?`, rest :: binary>>, ?`, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
### Catch all
|
||||
# Catch all
|
||||
|
||||
defp handle_inline(<<char, rest::binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char | buffer], acc, options)
|
||||
defp handle_inline(<<char, rest :: binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char|buffer], acc, options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<>>, _mark, buffer, acc, _options) do
|
||||
IO.iodata_to_binary(Enum.reverse([Enum.reverse(buffer) | acc]))
|
||||
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer)|acc])
|
||||
end
|
||||
|
||||
defp inline_buffer(buffer, options) do
|
||||
[mark | t] = Enum.reverse(buffer)
|
||||
inline_text(mark, t, options)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp quote_prefix(options), do: "#{color(:doc_quote, options)}> #{maybe_reset(options)}"
|
||||
|
||||
defp inline_text(mark, text, options) do
|
||||
if options[:enabled] do
|
||||
[[color_for(mark, options) | text] | IO.ANSI.reset()]
|
||||
else
|
||||
[[mark | text] | mark]
|
||||
end
|
||||
[h|t] = Enum.reverse([IO.ANSI.reset|buffer])
|
||||
[color_for(h, options)|t]
|
||||
end
|
||||
|
||||
defp color_for(mark, colors) do
|
||||
case mark do
|
||||
"__" -> color(:doc_bold, colors)
|
||||
"`" -> color(:doc_inline_code, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_bold, colors)
|
||||
"**" -> color(:doc_bold, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_underline, colors)
|
||||
"`" -> color(:doc_inline_code, colors)
|
||||
end
|
||||
end
|
||||
|
||||
defp bullet_text(options) do
|
||||
if options[:enabled], do: @bullet_text_unicode, else: @bullet_text_ascii
|
||||
end
|
||||
|
||||
defp color(style, colors) do
|
||||
IO.ANSI.format_fragment(colors[style], colors[:enabled])
|
||||
color = colors[style]
|
||||
IO.ANSI.format_fragment(color, colors[:enabled])
|
||||
end
|
||||
|
||||
defp newline_after_block(options) do
|
||||
IO.puts(maybe_reset(options))
|
||||
end
|
||||
|
||||
defp maybe_reset(options) do
|
||||
if options[:enabled], do: IO.ANSI.reset(), else: ""
|
||||
end
|
||||
defp newline_after_block, do: IO.puts(IO.ANSI.reset)
|
||||
end
|
||||
|
||||
+16
-25
@@ -1,34 +1,28 @@
|
||||
defmodule IO.StreamError do
|
||||
defexception [:reason]
|
||||
defexception [:reason, :message]
|
||||
|
||||
@impl true
|
||||
def message(%{reason: reason}) do
|
||||
"error during streaming: #{inspect(reason)}"
|
||||
def exception(opts) do
|
||||
reason = opts[:reason]
|
||||
formatted = IO.iodata_to_binary(:file.format_error(reason))
|
||||
%IO.StreamError{message: "error during streaming: #{formatted}", reason: reason}
|
||||
end
|
||||
end
|
||||
|
||||
defmodule IO.Stream do
|
||||
@moduledoc """
|
||||
Defines an `IO.Stream` struct returned by `IO.stream/2` and `IO.binstream/2`.
|
||||
Defines a `IO.Stream` struct returned by `IO.stream/2` and `IO.binstream/2`.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
* `device` - the IO device
|
||||
* `raw` - a boolean indicating if bin functions should be used
|
||||
* `line_or_bytes` - if reading should read lines or a given number of bytes
|
||||
|
||||
It is worth noting that an IO stream has side effects and every time you go
|
||||
over the stream you may get different results.
|
||||
* `line_or_bytes` - if reading should read lines or a given amount of bytes
|
||||
|
||||
"""
|
||||
|
||||
defstruct device: nil, raw: true, line_or_bytes: :line
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
device: IO.device(),
|
||||
raw: boolean(),
|
||||
line_or_bytes: :line | non_neg_integer()
|
||||
}
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc false
|
||||
def __build__(device, raw, line_or_bytes) do
|
||||
@@ -36,6 +30,10 @@ defmodule IO.Stream do
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def empty(stream) do
|
||||
stream
|
||||
end
|
||||
|
||||
def into(%{device: device, raw: raw} = stream) do
|
||||
{:ok, into(stream, device, raw)}
|
||||
end
|
||||
@@ -44,12 +42,10 @@ defmodule IO.Stream do
|
||||
fn
|
||||
:ok, {:cont, x} ->
|
||||
case raw do
|
||||
true -> IO.binwrite(device, x)
|
||||
true -> IO.binwrite(device, x)
|
||||
false -> IO.write(device, x)
|
||||
end
|
||||
|
||||
:ok, _ ->
|
||||
stream
|
||||
:ok, _ -> stream
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -58,11 +54,10 @@ defmodule IO.Stream do
|
||||
def reduce(%{device: device, raw: raw, line_or_bytes: line_or_bytes}, acc, fun) do
|
||||
next_fun =
|
||||
case raw do
|
||||
true -> &IO.each_binstream(&1, line_or_bytes)
|
||||
true -> &IO.each_binstream(&1, line_or_bytes)
|
||||
false -> &IO.each_stream(&1, line_or_bytes)
|
||||
end
|
||||
|
||||
Stream.resource(fn -> device end, next_fun, & &1).(acc, fun)
|
||||
Stream.resource(fn -> device end, next_fun, &(&1)).(acc, fun)
|
||||
end
|
||||
|
||||
def count(_stream) do
|
||||
@@ -72,9 +67,5 @@ defmodule IO.Stream do
|
||||
def member?(_stream, _term) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
def slice(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+1336
-4650
File diff suppressed because it is too large
Load Diff
+238
-374
@@ -1,45 +1,28 @@
|
||||
defmodule Kernel.CLI do
|
||||
@moduledoc false
|
||||
|
||||
@compile {:no_warn_undefined, [Logger, IEx]}
|
||||
|
||||
@blank_config %{
|
||||
commands: [],
|
||||
output: ".",
|
||||
compile: [],
|
||||
no_halt: false,
|
||||
compiler_options: [],
|
||||
errors: [],
|
||||
verbose_compile: false,
|
||||
profile: nil,
|
||||
pry: false,
|
||||
mode: :elixir
|
||||
}
|
||||
|
||||
@standalone_opts [~c"-h", ~c"--help", ~c"--short-version"]
|
||||
@blank_config %{commands: [], output: ".", compile: [],
|
||||
halt: true, compiler_options: [], errors: [],
|
||||
pa: [], pz: [], verbose_compile: false}
|
||||
|
||||
@doc """
|
||||
This is the API invoked by Elixir boot process.
|
||||
"""
|
||||
def main(argv) do
|
||||
argv = for arg <- argv, do: IO.chardata_to_string(arg)
|
||||
|
||||
{config, argv} = parse_argv(argv)
|
||||
System.argv(Enum.map(argv, &IO.chardata_to_string/1))
|
||||
System.no_halt(config.no_halt)
|
||||
:elixir_code_server.cast({:paths, config.pa, config.pz})
|
||||
System.argv(argv)
|
||||
|
||||
if config.pry do
|
||||
Application.put_env(:elixir, :dbg_callback, {IEx.Pry, :dbg, []})
|
||||
end
|
||||
|
||||
fun = fn _ ->
|
||||
run fn _ ->
|
||||
errors = process_commands(config)
|
||||
|
||||
if errors != [] do
|
||||
Enum.each(errors, &IO.puts(:stderr, &1))
|
||||
System.halt(1)
|
||||
end
|
||||
end
|
||||
|
||||
run(fun)
|
||||
end, config.halt
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -50,75 +33,26 @@ defmodule Kernel.CLI do
|
||||
This function is used by Elixir's CLI and also
|
||||
by escripts generated by Elixir.
|
||||
"""
|
||||
def run(fun) do
|
||||
{ok_or_shutdown, status} = exec_fun(fun, {:ok, 0})
|
||||
|
||||
if ok_or_shutdown == :shutdown or not System.no_halt() do
|
||||
{_, status} = at_exit({ok_or_shutdown, status})
|
||||
|
||||
# Ensure Logger messages are flushed before halting
|
||||
if Code.loaded?(Logger) do
|
||||
Logger.flush()
|
||||
end
|
||||
|
||||
System.halt(status)
|
||||
def run(fun, halt \\ true) do
|
||||
res = exec_fun(fun, {:ok, 0})
|
||||
if elem(res, 0) == :shutdown or halt do
|
||||
{_, int} = at_exit(res)
|
||||
System.halt(int)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the CLI arguments. Made public for testing.
|
||||
"""
|
||||
@doc false
|
||||
def parse_argv(argv) do
|
||||
parse_argv(argv, @blank_config)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Process CLI commands. Made public for testing.
|
||||
"""
|
||||
@doc false
|
||||
def process_commands(config) do
|
||||
commands =
|
||||
case config do
|
||||
%{mode: :elixirc, compile: compile, commands: commands} ->
|
||||
[{:compile, compile} | commands]
|
||||
|
||||
%{commands: commands} ->
|
||||
commands
|
||||
end
|
||||
|
||||
results = Enum.map(Enum.reverse(commands), &process_command(&1, config))
|
||||
errors = for {:error, msg} <- results, do: msg
|
||||
results = Enum.map(Enum.reverse(config.commands), &process_command(&1, config))
|
||||
errors = for {:error, msg} <- results, do: msg
|
||||
Enum.reverse(config.errors, errors)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Shared helper for error formatting on CLI tools.
|
||||
"""
|
||||
def format_error(kind, reason, stacktrace) do
|
||||
{blamed, stacktrace} = Exception.blame(kind, reason, stacktrace)
|
||||
|
||||
iodata =
|
||||
case blamed do
|
||||
%FunctionClauseError{} ->
|
||||
formatted = Exception.format_banner(kind, reason, stacktrace)
|
||||
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/1))
|
||||
[formatted, padded_blame]
|
||||
|
||||
_ ->
|
||||
Exception.format_banner(kind, blamed, stacktrace)
|
||||
end
|
||||
|
||||
[iodata, ?\n, Exception.format_stacktrace(prune_stacktrace(stacktrace))]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Function invoked across nodes for `--rpc-eval`.
|
||||
"""
|
||||
def rpc_eval(expr) do
|
||||
wrapper(fn -> Code.eval_string(expr) end)
|
||||
catch
|
||||
kind, reason -> {kind, reason, __STACKTRACE__}
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp at_exit(res) do
|
||||
@@ -131,248 +65,160 @@ defmodule Kernel.CLI do
|
||||
parent = self()
|
||||
|
||||
{pid, ref} =
|
||||
spawn_monitor(fn ->
|
||||
spawn_monitor fn ->
|
||||
try do
|
||||
fun.(elem(res, 1))
|
||||
catch
|
||||
:exit, {:shutdown, int} when is_integer(int) ->
|
||||
send(parent, {self(), {:shutdown, int}})
|
||||
send parent, {self, {:shutdown, int}}
|
||||
exit({:shutdown, int})
|
||||
|
||||
:exit, reason
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown ->
|
||||
send(parent, {self(), {:shutdown, 0}})
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown ->
|
||||
send parent, {self, {:shutdown, 0}}
|
||||
exit(reason)
|
||||
|
||||
kind, reason ->
|
||||
print_error(kind, reason, __STACKTRACE__)
|
||||
send(parent, {self(), {:shutdown, 1}})
|
||||
exit(to_exit(kind, reason, __STACKTRACE__))
|
||||
stack = System.stacktrace
|
||||
print_error(kind, reason, stack)
|
||||
send parent, {self, {:shutdown, 1}}
|
||||
:erlang.raise(kind, reason, stack)
|
||||
else
|
||||
_ ->
|
||||
send(parent, {self(), res})
|
||||
send parent, {self, res}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
receive do
|
||||
{^pid, res} ->
|
||||
:erlang.demonitor(ref, [:flush])
|
||||
res
|
||||
|
||||
{:DOWN, ^ref, _, _, other} ->
|
||||
print_error({:EXIT, pid}, other, [])
|
||||
{:shutdown, 1}
|
||||
end
|
||||
end
|
||||
|
||||
defp to_exit(:throw, reason, stack), do: {{:nocatch, reason}, stack}
|
||||
defp to_exit(:error, reason, stack), do: {reason, stack}
|
||||
defp to_exit(:exit, reason, _stack), do: reason
|
||||
|
||||
## Error handling
|
||||
|
||||
defp print_error(kind, reason, stacktrace) do
|
||||
IO.write(:stderr, format_error(kind, reason, stacktrace))
|
||||
end
|
||||
|
||||
defp blame_match(%{match?: true, node: node}), do: blame_ansi(:normal, "+", node)
|
||||
defp blame_match(%{match?: false, node: node}), do: blame_ansi(:red, "-", node)
|
||||
|
||||
defp blame_ansi(color, no_ansi, node) do
|
||||
if IO.ANSI.enabled?() do
|
||||
[color | Macro.to_string(node)]
|
||||
|> IO.ANSI.format(true)
|
||||
|> IO.iodata_to_binary()
|
||||
else
|
||||
no_ansi <> Macro.to_string(node) <> no_ansi
|
||||
defp shared_option?(list, config, callback) do
|
||||
case parse_shared(list, config) do
|
||||
{[h|hs], _} when h == hd(list) ->
|
||||
new_config = %{config | errors: ["#{h} : Unknown option" | config.errors]}
|
||||
callback.(hs, new_config)
|
||||
{new_list, new_config} ->
|
||||
callback.(new_list, new_config)
|
||||
end
|
||||
end
|
||||
|
||||
defp pad(string) do
|
||||
" " <> String.replace(string, "\n", "\n ")
|
||||
defp print_error(kind, reason, trace) do
|
||||
IO.puts :stderr, Exception.format(kind, reason, prune_stacktrace(trace))
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
|
||||
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map, :elixir_locals] ++
|
||||
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_compiler, :elixir_erl_pass] ++
|
||||
[Kernel.ErrorHandler, Module.ParallelChecker]
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_translator,
|
||||
:elixir_expand, :elixir_lexical]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _} | _]) do
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _}|_]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h | t]) do
|
||||
[h | prune_stacktrace(t)]
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
# Parse shared options
|
||||
|
||||
defp parse_shared([opt|_t], _config) when opt in ["-v", "--version"] do
|
||||
IO.puts "Elixir #{System.version}"
|
||||
System.halt 0
|
||||
end
|
||||
|
||||
defp parse_shared(["-pa", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_patha/1)
|
||||
parse_shared t, %{config | pa: config.pa ++ paths}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pz", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_pathz/1)
|
||||
parse_shared t, %{config | pz: config.pz ++ paths}
|
||||
end
|
||||
|
||||
defp parse_shared(["--app", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:app, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["--no-halt"|t], config) do
|
||||
parse_shared t, %{config | halt: false}
|
||||
end
|
||||
|
||||
defp parse_shared(["-e", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:eval, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-r", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pr", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:parallel_require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared([erl, _|t], config) when erl in ["--erl", "--sname", "--name", "--cookie"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared([erl|t], config) when erl in ["--detached", "--hidden", "--werl"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared(list, config) do
|
||||
{list, config}
|
||||
end
|
||||
|
||||
defp expand_code_path(path) do
|
||||
path = Path.expand(path)
|
||||
case Path.wildcard(path) do
|
||||
[] -> [to_char_list(path)]
|
||||
list -> Enum.map(list, &to_char_list/1)
|
||||
end
|
||||
end
|
||||
|
||||
# Process init options
|
||||
|
||||
defp parse_argv([~c"--" | t], config) do
|
||||
defp parse_argv(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp parse_argv([~c"+elixirc" | t], config) do
|
||||
parse_argv(t, %{config | mode: :elixirc})
|
||||
defp parse_argv(["+elixirc"|t], config) do
|
||||
parse_compiler t, config
|
||||
end
|
||||
|
||||
defp parse_argv([~c"+iex" | t], config) do
|
||||
parse_argv(t, %{config | mode: :iex})
|
||||
defp parse_argv(["+iex"|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_argv([~c"-S", h | t], config) do
|
||||
defp parse_argv(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_argv([opt | _], _config) when opt in @standalone_opts do
|
||||
halt_standalone(opt)
|
||||
end
|
||||
|
||||
defp parse_argv([opt | t], config) when opt in [~c"-v", ~c"--version"] do
|
||||
if config.mode == :iex do
|
||||
IO.puts("IEx " <> System.build_info()[:build])
|
||||
else
|
||||
IO.puts(:erlang.system_info(:system_version))
|
||||
IO.puts("Elixir " <> System.build_info()[:build])
|
||||
end
|
||||
|
||||
if t != [] do
|
||||
halt_standalone(opt)
|
||||
else
|
||||
System.halt(0)
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_argv([~c"-pa", h | t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Code.prepend_paths(paths)
|
||||
parse_argv(t, config)
|
||||
end
|
||||
|
||||
defp parse_argv([~c"-pz", h | t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Code.append_paths(paths)
|
||||
parse_argv(t, config)
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--no-halt" | t], config) do
|
||||
parse_argv(t, %{config | no_halt: true})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"-e", h | t], config) do
|
||||
parse_argv(t, %{config | commands: [{:eval, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--eval", h | t], config) do
|
||||
parse_argv(t, %{config | commands: [{:eval, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--rpc-eval", node, h | t], config) do
|
||||
node = append_hostname(node)
|
||||
parse_argv(t, %{config | commands: [{:rpc_eval, node, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--rpc-eval" | _], config) do
|
||||
new_config = %{config | errors: ["--rpc-eval : wrong number of arguments" | config.errors]}
|
||||
{new_config, []}
|
||||
end
|
||||
|
||||
defp parse_argv([~c"-r", h | t], config) do
|
||||
parse_argv(t, %{config | commands: [{:require, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"-pr", h | t], config) do
|
||||
parse_argv(t, %{config | commands: [{:parallel_require, h} | config.commands]})
|
||||
end
|
||||
|
||||
## Compiler
|
||||
|
||||
defp parse_argv([~c"-o", h | t], %{mode: :elixirc} = config) do
|
||||
parse_argv(t, %{config | output: h})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--no-docs" | t], %{mode: :elixirc} = config) do
|
||||
parse_argv(t, %{config | compiler_options: [{:docs, false} | config.compiler_options]})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--no-debug-info" | t], %{mode: :elixirc} = config) do
|
||||
compiler_options = [{:debug_info, false} | config.compiler_options]
|
||||
parse_argv(t, %{config | compiler_options: compiler_options})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--ignore-module-conflict" | t], %{mode: :elixirc} = config) do
|
||||
compiler_options = [{:ignore_module_conflict, true} | config.compiler_options]
|
||||
parse_argv(t, %{config | compiler_options: compiler_options})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--warnings-as-errors" | t], %{mode: :elixirc} = config) do
|
||||
compiler_options = [{:warnings_as_errors, true} | config.compiler_options]
|
||||
parse_argv(t, %{config | compiler_options: compiler_options})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--verbose" | t], %{mode: :elixirc} = config) do
|
||||
parse_argv(t, %{config | verbose_compile: true})
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--profile", "time" | t], %{mode: :elixirc} = config) do
|
||||
parse_argv(t, %{config | profile: :time})
|
||||
end
|
||||
|
||||
## IEx
|
||||
|
||||
defp parse_argv([~c"--dbg", backend | t], %{mode: :iex} = config) do
|
||||
case backend do
|
||||
~c"pry" ->
|
||||
parse_argv(t, %{config | pry: true})
|
||||
|
||||
~c"kernel" ->
|
||||
parse_argv(t, %{config | pry: false})
|
||||
|
||||
defp parse_argv([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_argv(&1, &2)
|
||||
_ ->
|
||||
error = "--dbg : Unknown dbg backend #{inspect(backend)}"
|
||||
parse_argv(t, %{config | errors: [error | config.errors]})
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_argv([~c"--dot-iex", _ | t], %{mode: :iex} = config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--remsh", _ | t], %{mode: :iex} = config), do: parse_argv(t, config)
|
||||
|
||||
## Erlang flags
|
||||
|
||||
defp parse_argv([~c"--boot", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--boot-var", _, _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--cookie", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--hidden" | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--erl-config", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--logger-otp-reports", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--logger-sasl-reports", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--name", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--sname", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--vm-args", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--erl", _ | t], config), do: parse_argv(t, config)
|
||||
defp parse_argv([~c"--pipe-to", _, _ | t], config), do: parse_argv(t, config)
|
||||
|
||||
## Fallback
|
||||
|
||||
defp parse_argv([h | t], %{mode: :elixirc} = config) do
|
||||
pattern = if File.dir?(h), do: "#{h}/**/*.ex", else: h
|
||||
parse_argv(t, %{config | compile: [pattern | config.compile]})
|
||||
end
|
||||
|
||||
defp parse_argv([h | t], config) do
|
||||
if List.keymember?(config.commands, :eval, 0) do
|
||||
{config, [h | t]}
|
||||
else
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
if Keyword.has_key?(config.commands, :eval) do
|
||||
{config, list}
|
||||
else
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -380,98 +226,138 @@ defmodule Kernel.CLI do
|
||||
{config, []}
|
||||
end
|
||||
|
||||
# Parse helpers
|
||||
# Parse compiler options
|
||||
|
||||
defp halt_standalone(opt) do
|
||||
IO.puts(:stderr, "#{opt} : Standalone options can't be combined with other options")
|
||||
System.halt(1)
|
||||
defp parse_compiler(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp append_hostname(node) do
|
||||
with false <- ?@ in node,
|
||||
[_ | _] = suffix <- :string.find(Atom.to_charlist(:net_kernel.nodename()), ~c"@") do
|
||||
node ++ suffix
|
||||
else
|
||||
_ -> node
|
||||
defp parse_compiler(["-o", h|t], config) do
|
||||
parse_compiler t, %{config | output: h}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-docs"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:docs, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-debug-info"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:debug_info, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--ignore-module-conflict"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:ignore_module_conflict, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--warnings-as-errors"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:warnings_as_errors, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--verbose"|t], config) do
|
||||
parse_compiler t, %{config | verbose_compile: true}
|
||||
end
|
||||
|
||||
defp parse_compiler([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_compiler(&1, &2)
|
||||
_ ->
|
||||
pattern = if File.dir?(h), do: "#{h}/**/*.ex", else: h
|
||||
parse_compiler t, %{config | compile: [pattern | config.compile]}
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_code_path(path) do
|
||||
path = Path.expand(path)
|
||||
defp parse_compiler([], config) do
|
||||
{%{config | commands: [{:compile, config.compile}|config.commands]}, []}
|
||||
end
|
||||
|
||||
case Path.wildcard(path) do
|
||||
[] -> [to_charlist(path)]
|
||||
list -> Enum.map(list, &to_charlist/1)
|
||||
# Parse iex options
|
||||
|
||||
defp parse_iex(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
# This clause is here so that Kernel.CLI does not
|
||||
# error out with "unknown option"
|
||||
defp parse_iex(["--dot-iex", _|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex([opt, _|t], config) when opt in ["--remsh"] do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_iex([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_iex(&1, &2)
|
||||
_ ->
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_iex([], config) do
|
||||
{config, []}
|
||||
end
|
||||
|
||||
# Process commands
|
||||
|
||||
defp process_command({:eval, expr}, _config) when is_list(expr) do
|
||||
wrapper(fn -> Code.eval_string(expr, []) end)
|
||||
end
|
||||
|
||||
defp process_command({:rpc_eval, node, expr}, _config) when is_list(expr) do
|
||||
node = List.to_atom(node)
|
||||
|
||||
# Explicitly connect the node in case the rpc node was started with --sname/--name undefined.
|
||||
_ = :net_kernel.connect_node(node)
|
||||
|
||||
try do
|
||||
:erpc.call(node, __MODULE__, :rpc_eval, [expr])
|
||||
catch
|
||||
:error, {:erpc, reason} ->
|
||||
if reason == :noconnection and :net_kernel.nodename() == :ignored do
|
||||
{:error,
|
||||
"--rpc-eval : Cannot run --rpc-eval if the node is not alive (set --name or --sname)"}
|
||||
else
|
||||
{:error, "--rpc-eval : RPC failed with reason #{inspect(reason)}"}
|
||||
end
|
||||
|
||||
:exit, {kind, exit} when kind in [:exception, :signal] ->
|
||||
Process.exit(self(), exit)
|
||||
defp process_command({:cookie, h}, _config) do
|
||||
if Node.alive? do
|
||||
wrapper fn -> Node.set_cookie(String.to_atom(h)) end
|
||||
else
|
||||
:ok -> :ok
|
||||
{kind, reason, stack} -> :erlang.raise(kind, reason, stack)
|
||||
{:error, "--cookie : Cannot set cookie if the node is not alive (set --name or --sname)"}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:script, file}, _config) when is_list(file) do
|
||||
defp process_command({:eval, expr}, _config) when is_binary(expr) do
|
||||
wrapper fn -> Code.eval_string(expr, []) end
|
||||
end
|
||||
|
||||
defp process_command({:app, app}, _config) when is_binary(app) do
|
||||
case Application.ensure_all_started(String.to_atom(app)) do
|
||||
{:error, {app, reason}} ->
|
||||
{:error, "--app : Could not start application #{app}: " <>
|
||||
Application.format_error(reason)}
|
||||
{:ok, _} ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:script, file}, _config) when is_binary(file) do
|
||||
if exec = find_elixir_executable(file) do
|
||||
wrapper(fn -> Code.require_file(IO.chardata_to_string(exec)) end)
|
||||
wrapper fn -> Code.require_file(exec) end
|
||||
else
|
||||
{:error, "-S : Could not find executable #{file}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:file, file}, _config) when is_list(file) do
|
||||
defp process_command({:file, file}, _config) when is_binary(file) do
|
||||
if File.regular?(file) do
|
||||
wrapper(fn -> Code.require_file(IO.chardata_to_string(file)) end)
|
||||
wrapper fn -> Code.require_file(file) end
|
||||
else
|
||||
{:error, "No file named #{file}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:require, pattern}, _config) when is_list(pattern) do
|
||||
defp process_command({:require, pattern}, _config) when is_binary(pattern) do
|
||||
files = filter_patterns(pattern)
|
||||
|
||||
if files != [] do
|
||||
wrapper(fn -> Enum.map(files, &Code.require_file/1) end)
|
||||
wrapper fn -> Enum.map files, &Code.require_file(&1) end
|
||||
else
|
||||
{:error, "-r : No files matched pattern #{pattern}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:parallel_require, pattern}, _config) when is_list(pattern) do
|
||||
defp process_command({:parallel_require, pattern}, _config) when is_binary(pattern) do
|
||||
files = filter_patterns(pattern)
|
||||
|
||||
if files != [] do
|
||||
wrapper(fn ->
|
||||
case Kernel.ParallelCompiler.require(files) do
|
||||
{:ok, _, _} -> :ok
|
||||
{:error, _, _} -> exit({:shutdown, 1})
|
||||
end
|
||||
end)
|
||||
wrapper fn -> Kernel.ParallelRequire.files(files) end
|
||||
else
|
||||
{:error, "-pr : No files matched pattern #{pattern}"}
|
||||
end
|
||||
@@ -479,67 +365,46 @@ defmodule Kernel.CLI do
|
||||
|
||||
defp process_command({:compile, patterns}, config) do
|
||||
# If ensuring the dir returns an error no files will be found.
|
||||
_ = :filelib.ensure_path(config.output)
|
||||
_ = :filelib.ensure_dir(:filename.join(config.output, "."))
|
||||
|
||||
case filter_multiple_patterns(patterns) do
|
||||
{:ok, []} ->
|
||||
{:error, "No files matched provided patterns"}
|
||||
|
||||
{:ok, files} ->
|
||||
wrapper(fn ->
|
||||
wrapper fn ->
|
||||
Code.compiler_options(config.compiler_options)
|
||||
|
||||
verbose_opts =
|
||||
if config.verbose_compile do
|
||||
[each_file: &IO.puts("Compiling #{Path.relative_to_cwd(&1)}")]
|
||||
else
|
||||
[
|
||||
each_long_compilation:
|
||||
&IO.puts("Compiling #{Path.relative_to_cwd(&1)} (it's taking more than 10s)")
|
||||
]
|
||||
end
|
||||
|
||||
profile_opts =
|
||||
if config.profile do
|
||||
[profile: config.profile]
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
output = IO.chardata_to_string(config.output)
|
||||
opts = verbose_opts ++ profile_opts
|
||||
|
||||
case Kernel.ParallelCompiler.compile_to_path(files, output, opts) do
|
||||
{:ok, _, _} -> :ok
|
||||
{:error, _, _} -> exit({:shutdown, 1})
|
||||
end
|
||||
end)
|
||||
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output,
|
||||
each_file: fn file -> if config.verbose_compile do IO.puts "Compiled #{file}" end end)
|
||||
end
|
||||
{:missing, missing} ->
|
||||
{:error, "No files matched pattern(s) #{Enum.join(missing, ",")}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp filter_patterns(pattern) do
|
||||
pattern
|
||||
|> Path.expand()
|
||||
|> Path.wildcard()
|
||||
|> :lists.usort()
|
||||
|> Enum.filter(&File.regular?/1)
|
||||
Enum.filter(Enum.uniq(Path.wildcard(pattern)), &File.regular?(&1))
|
||||
end
|
||||
|
||||
defp filter_multiple_patterns(patterns) do
|
||||
{files, missing} =
|
||||
Enum.reduce(patterns, {[], []}, fn pattern, {files, missing} ->
|
||||
case filter_patterns(pattern) do
|
||||
[] -> {files, [pattern | missing]}
|
||||
match -> {match ++ files, missing}
|
||||
end
|
||||
end)
|
||||
matched_files = Enum.map patterns, fn(pattern) ->
|
||||
case filter_patterns(pattern) do
|
||||
[] -> {:missing, pattern}
|
||||
files -> {:ok, files}
|
||||
end
|
||||
end
|
||||
|
||||
case missing do
|
||||
[] -> {:ok, :lists.usort(files)}
|
||||
_ -> {:missing, :lists.usort(missing)}
|
||||
files = Enum.filter_map matched_files,
|
||||
fn(match) -> elem(match, 0) == :ok end,
|
||||
&elem(&1, 1)
|
||||
|
||||
missing_patterns = Enum.filter_map matched_files,
|
||||
fn(match) -> elem(match, 0) == :missing end,
|
||||
&elem(&1, 1)
|
||||
|
||||
if missing_patterns == [] do
|
||||
{:ok, Enum.uniq(Enum.concat(files))}
|
||||
else
|
||||
{:missing, Enum.uniq(missing_patterns)}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -549,15 +414,14 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
defp find_elixir_executable(file) do
|
||||
if exec = :os.find_executable(file) do
|
||||
if exec = System.find_executable(file) do
|
||||
# If we are on Windows, the executable is going to be
|
||||
# a .bat file that must be in the same directory as
|
||||
# the actual Elixir executable.
|
||||
case :os.type() do
|
||||
{:win32, _} ->
|
||||
base = :filename.rootname(exec)
|
||||
base = Path.rootname(exec)
|
||||
if File.regular?(base), do: base, else: exec
|
||||
|
||||
_ ->
|
||||
exec
|
||||
end
|
||||
|
||||
@@ -3,41 +3,38 @@
|
||||
defmodule Kernel.ErrorHandler do
|
||||
@moduledoc false
|
||||
|
||||
@spec undefined_function(module, atom, list) :: term
|
||||
def undefined_function(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module, :raise)
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_function(module, fun, args)
|
||||
end
|
||||
|
||||
@spec undefined_lambda(module, fun, list) :: term
|
||||
def undefined_lambda(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module, :raise)
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
@spec ensure_loaded(module) :: boolean
|
||||
def ensure_loaded(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:module, _} -> true
|
||||
{:error, _} -> false
|
||||
end
|
||||
def release() do
|
||||
# On release, no further allow elixir_ensure_compiled
|
||||
# directives and revert to the original error handler.
|
||||
# Note we should not delete the elixir_compiler_pid though,
|
||||
# as we still want to send notifications to the compiler.
|
||||
:erlang.erase(:elixir_ensure_compiled)
|
||||
:erlang.process_flag(:error_handler, :error_handler)
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec ensure_compiled(module, atom, atom) :: :found | :not_found | :deadlock
|
||||
# Never wait on nil because it should never be defined.
|
||||
def ensure_compiled(nil, _kind, _deadlock) do
|
||||
:not_found
|
||||
end
|
||||
|
||||
def ensure_compiled(module, kind, deadlock) do
|
||||
{compiler_pid, file_pid} = :erlang.get(:elixir_compiler_info)
|
||||
ref = :erlang.make_ref()
|
||||
modules = :elixir_module.compiler_modules()
|
||||
send(compiler_pid, {:waiting, kind, self(), ref, file_pid, module, modules, deadlock})
|
||||
:erlang.garbage_collect(self())
|
||||
|
||||
receive do
|
||||
{^ref, value} -> value
|
||||
defp ensure_loaded(module) do
|
||||
case Code.ensure_loaded(module) do
|
||||
{:module, _} -> :ok
|
||||
{:error, _} ->
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref
|
||||
send parent, {:waiting, :module, self(), ref, module}
|
||||
:erlang.garbage_collect(self)
|
||||
receive do
|
||||
{^ref, :ready} -> :ok
|
||||
{^ref, :release} -> release()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,234 +1,155 @@
|
||||
# This is an Elixir module responsible for tracking references
|
||||
# to modules, remote dispatches, and the usage of
|
||||
# aliases/imports/requires in the Elixir scope.
|
||||
# This is a module Elixir responsible for tracking
|
||||
# the usage of aliases, imports and requires in the Elixir scope.
|
||||
#
|
||||
# The implementation simply stores dispatch information in an
|
||||
# ETS table and then consults this table once compilation is done.
|
||||
#
|
||||
# Note that since this is required for bootstrap, we can't use
|
||||
# any of the `GenServer.Behaviour` conveniences.
|
||||
defmodule Kernel.LexicalTracker do
|
||||
@moduledoc false
|
||||
@timeout :infinity
|
||||
@timeout 30_000
|
||||
@behaviour :gen_server
|
||||
|
||||
@import 2
|
||||
@alias 3
|
||||
|
||||
@doc """
|
||||
Returns all references in this lexical scope.
|
||||
Returns all remotes linked to in this lexical scope.
|
||||
"""
|
||||
def references(pid) do
|
||||
:gen_server.call(pid, :references, @timeout)
|
||||
def remotes(arg) do
|
||||
ets = :gen_server.call(to_pid(arg), :ets, @timeout)
|
||||
:ets.match(ets, {:"$1", :_, :_}) |> List.flatten
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the destination the lexical scope is meant to
|
||||
compile to.
|
||||
"""
|
||||
def dest(arg) do
|
||||
:gen_server.call(to_pid(arg), :dest, @timeout)
|
||||
end
|
||||
|
||||
defp to_pid(pid) when is_pid(pid), do: pid
|
||||
defp to_pid(mod) when is_atom(mod) do
|
||||
table = :elixir_module.data_table(mod)
|
||||
[{_, val}] = :ets.lookup(table, {:elixir, :lexical_tracker})
|
||||
val
|
||||
end
|
||||
|
||||
# Internal API
|
||||
|
||||
# Starts the tracker and returns its PID.
|
||||
# Starts the tracker and returns its pid.
|
||||
@doc false
|
||||
def start_link() do
|
||||
:gen_server.start_link(__MODULE__, :ok, [])
|
||||
def start_link(dest) do
|
||||
:gen_server.start_link(__MODULE__, dest, [])
|
||||
end
|
||||
|
||||
@doc false
|
||||
def stop(pid) do
|
||||
:gen_server.call(pid, :stop)
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_export(pid, module) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:add_export, module})
|
||||
def add_import(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_import, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_import(pid, module, fas, meta, warn) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:add_import, module, fas, meta, warn})
|
||||
def add_alias(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_alias, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_alias(pid, module, meta, warn) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:add_alias, module, meta, warn})
|
||||
def remote_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_dispatch(pid, module, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, mode})
|
||||
def import_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_dispatch(pid, module, fa, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module, fa, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def alias_dispatch(pid, module) when is_atom(module) do
|
||||
def alias_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:alias_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_quoted(pid, module, function, arities) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:import_quoted, module, function, arities})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_compile_env(pid, app, path, return) do
|
||||
:gen_server.cast(pid, {:compile_env, app, path, return})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def set_file(pid, file) do
|
||||
:gen_server.cast(pid, {:set_file, file})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def reset_file(pid) do
|
||||
:gen_server.cast(pid, :reset_file)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def write_cache(pid, value) do
|
||||
key = :erlang.unique_integer()
|
||||
:gen_server.cast(pid, {:write_cache, key, value})
|
||||
key
|
||||
end
|
||||
|
||||
@doc false
|
||||
def read_cache(pid, key) do
|
||||
:gen_server.call(pid, {:read_cache, key}, @timeout)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def collect_unused_imports(pid) do
|
||||
unused(pid, :unused_imports)
|
||||
unused(pid, @import)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def collect_unused_aliases(pid) do
|
||||
unused(pid, :unused_aliases)
|
||||
unused(pid, @alias)
|
||||
end
|
||||
|
||||
defp unused(pid, tag) do
|
||||
:gen_server.call(pid, tag, @timeout)
|
||||
defp unused(pid, pos) do
|
||||
ets = :gen_server.call(pid, :ets, @timeout)
|
||||
:ets.foldl(fn
|
||||
{module, _, _} = tuple, acc when is_integer(:erlang.element(pos, tuple)) ->
|
||||
[{module, :erlang.element(pos, tuple)}|acc]
|
||||
_, acc ->
|
||||
acc
|
||||
end, [], ets) |> Enum.sort
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
def init(:ok) do
|
||||
state = %{
|
||||
aliases: %{},
|
||||
imports: %{},
|
||||
references: %{},
|
||||
exports: %{},
|
||||
cache: %{},
|
||||
compile_env: :ordsets.new(),
|
||||
file: nil
|
||||
}
|
||||
|
||||
{:ok, state}
|
||||
def init(dest) do
|
||||
{:ok, {:ets.new(:lexical, [:protected]), dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call(:unused_aliases, _from, state) do
|
||||
{:reply, Enum.sort(state.aliases), state}
|
||||
def handle_call(:ets, _from, {d, dest}) do
|
||||
{:reply, d, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:unused_imports, _from, state) do
|
||||
{:reply, Enum.sort(state.imports), state}
|
||||
def handle_call(:dest, _from, {d, dest}) do
|
||||
{:reply, dest, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:references, _from, state) do
|
||||
{compile, runtime} = partition(Map.to_list(state.references), [], [])
|
||||
{:reply, {compile, Map.keys(state.exports), runtime, state.compile_env}, state}
|
||||
def handle_call(request, _from, {d, dest}) do
|
||||
{:stop, {:bad_call, request}, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
|
||||
{:reply, Map.get(cache, key), state}
|
||||
def handle_cast({:remote_dispatch, module}, {d, dest}) do
|
||||
add_module(d, module)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:stop, _from, state) do
|
||||
{:stop, :normal, :ok, state}
|
||||
def handle_cast({:import_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, @import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:write_cache, key, value}, %{cache: cache} = state) do
|
||||
{:noreply, %{state | cache: Map.put(cache, key, value)}}
|
||||
def handle_cast({:alias_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, @alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, mode}, state) do
|
||||
references = add_reference(state.references, module, mode)
|
||||
{:noreply, %{state | references: references}}
|
||||
def handle_cast({:add_import, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, @import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:import_dispatch, module, {function, arity}, mode}, state) do
|
||||
%{imports: imports, references: references} = state
|
||||
|
||||
imports =
|
||||
case imports do
|
||||
%{^module => modules_and_fas} ->
|
||||
modules_and_fas
|
||||
|> Map.delete(module)
|
||||
|> Map.delete({function, arity})
|
||||
|> then(&Map.put(imports, module, &1))
|
||||
|
||||
%{} ->
|
||||
imports
|
||||
end
|
||||
|
||||
references = add_reference(references, module, mode)
|
||||
{:noreply, %{state | imports: imports, references: references}}
|
||||
def handle_cast({:add_alias, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, @alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:alias_dispatch, module}, %{aliases: aliases} = state) do
|
||||
{:noreply, %{state | aliases: Map.delete(aliases, module)}}
|
||||
def handle_cast(:stop, {d, dest}) do
|
||||
{:stop, :normal, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:import_quoted, module, function, arities}, state) do
|
||||
%{imports: imports} = state
|
||||
|
||||
imports =
|
||||
case imports do
|
||||
%{^module => modules_and_fas} ->
|
||||
arities
|
||||
|> Enum.reduce(modules_and_fas, &Map.delete(&2, {function, &1}))
|
||||
|> Map.delete(module)
|
||||
|> then(&Map.put(imports, module, &1))
|
||||
|
||||
%{} ->
|
||||
imports
|
||||
end
|
||||
|
||||
{:noreply, %{state | imports: imports}}
|
||||
end
|
||||
|
||||
def handle_cast({:set_file, file}, state) do
|
||||
{:noreply, %{state | file: file}}
|
||||
end
|
||||
|
||||
def handle_cast(:reset_file, state) do
|
||||
{:noreply, %{state | file: nil}}
|
||||
end
|
||||
|
||||
def handle_cast({:compile_env, app, path, return}, state) do
|
||||
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
|
||||
end
|
||||
|
||||
def handle_cast({:add_export, module}, state) do
|
||||
{:noreply, put_in(state.exports[module], true)}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, fas, meta, warn}, state) do
|
||||
if warn do
|
||||
imports = for module_or_fa <- [module | fas], do: {module_or_fa, meta}, into: %{}
|
||||
{:noreply, put_in(state.imports[module], imports)}
|
||||
else
|
||||
{:noreply, state}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_cast({:add_alias, module, meta, warn}, state) do
|
||||
if warn do
|
||||
{:noreply, put_in(state.aliases[module], meta)}
|
||||
else
|
||||
{:noreply, state}
|
||||
end
|
||||
def handle_cast(msg, {d, dest}) do
|
||||
{:stop, {:bad_cast, msg}, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
def handle_info(_msg, {d, dest}) do
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -241,23 +162,23 @@ defmodule Kernel.LexicalTracker do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
defp partition([{remote, :compile} | t], compile, runtime),
|
||||
do: partition(t, [remote | compile], runtime)
|
||||
|
||||
defp partition([{remote, :runtime} | t], compile, runtime),
|
||||
do: partition(t, compile, [remote | runtime])
|
||||
|
||||
defp partition([], compile, runtime), do: {compile, runtime}
|
||||
|
||||
# Callbacks helpers
|
||||
|
||||
defp add_reference(references, module, :compile) when is_atom(module),
|
||||
do: Map.put(references, module, :compile)
|
||||
# In the table we keep imports and aliases.
|
||||
# If the value is false, it was not imported/aliased
|
||||
# If the value is true, it was imported/aliased
|
||||
# If the value is a line, it was imported/aliased and has a pending warning
|
||||
defp add_module(d, module) do
|
||||
:ets.insert_new(d, {module, false, false})
|
||||
end
|
||||
|
||||
defp add_reference(references, module, :runtime) when is_atom(module) do
|
||||
case references do
|
||||
%{^module => _} -> references
|
||||
%{} -> Map.put(references, module, :runtime)
|
||||
end
|
||||
defp add_dispatch(d, module, pos) do
|
||||
:ets.update_element(d, module, {pos, true})
|
||||
end
|
||||
|
||||
defp add_directive(d, module, line, warn, pos) do
|
||||
add_module(d, module)
|
||||
marker = if warn, do: line, else: true
|
||||
:ets.update_element(d, module, {pos, marker})
|
||||
end
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,17 +1,80 @@
|
||||
defmodule Kernel.ParallelRequire do
|
||||
@moduledoc false
|
||||
@moduledoc """
|
||||
A module responsible for requiring files in parallel.
|
||||
"""
|
||||
|
||||
@deprecated "Use Kernel.ParallelCompiler.require/2 instead"
|
||||
def files(files, callbacks \\ [])
|
||||
defmacrop default_callback, do: quote(do: fn x -> x end)
|
||||
|
||||
def files(files, callback) when is_function(callback, 1) do
|
||||
files(files, each_file: callback)
|
||||
@doc """
|
||||
Requires the given files.
|
||||
|
||||
A callback that is invoked every time a file is required
|
||||
can be optionally given as argument.
|
||||
|
||||
Returns the modules generated by each required file.
|
||||
"""
|
||||
def files(files, callback \\ default_callback) do
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
spawn_requires(files, [], callback, schedulers, [])
|
||||
end
|
||||
|
||||
def files(files, options) when is_list(options) do
|
||||
case Kernel.ParallelCompiler.require(files, options) do
|
||||
{:ok, modules, _} -> modules
|
||||
{:error, _, _} -> exit({:shutdown, 1})
|
||||
defp spawn_requires([], [], _callback, _schedulers, result), do: result
|
||||
|
||||
defp spawn_requires([], waiting, callback, schedulers, result) do
|
||||
wait_for_messages([], waiting, callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_requires(files, waiting, callback, schedulers, result) when length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_requires([h|t], waiting, callback, schedulers, result) do
|
||||
parent = self
|
||||
|
||||
compiler_pid = :erlang.get(:elixir_compiler_pid)
|
||||
ensure_compiled = :erlang.get(:elixir_ensure_compiled)
|
||||
{:error_handler, handler} = :erlang.process_info(parent, :error_handler)
|
||||
|
||||
{pid, ref} = :erlang.spawn_monitor fn ->
|
||||
if compiler_pid != :undefined do
|
||||
:erlang.put(:elixir_compiler_pid, compiler_pid)
|
||||
end
|
||||
|
||||
if ensure_compiled != :undefined do
|
||||
:erlang.put(:elixir_ensure_compiled, ensure_compiled)
|
||||
end
|
||||
|
||||
:erlang.process_flag(:error_handler, handler)
|
||||
|
||||
exit(try do
|
||||
new = Code.require_file(h) || []
|
||||
{:required, Enum.map(new, &elem(&1, 0)), h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
end
|
||||
|
||||
spawn_requires(t, [{pid, ref}|waiting], callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp wait_for_messages(files, waiting, callback, schedulers, result) do
|
||||
receive do
|
||||
{:DOWN, ref, :process, pid, status} ->
|
||||
tuple = {pid, ref}
|
||||
if tuple in waiting do
|
||||
case status do
|
||||
{:required, mods, file} ->
|
||||
callback.(file)
|
||||
result = mods ++ result
|
||||
waiting = List.delete(waiting, tuple)
|
||||
{:failure, kind, reason, stacktrace} ->
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
other ->
|
||||
:erlang.raise(:exit, other, [])
|
||||
end
|
||||
end
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user