|
1 |
| -import { DependencyType, ProjectGraph } from '../config/project-graph'; |
| 1 | +import { |
| 2 | + DependencyType, |
| 3 | + ProjectGraph, |
| 4 | + ProjectGraphProjectNode, |
| 5 | +} from '../config/project-graph'; |
| 6 | +import { ProjectConfiguration } from '../config/workspace-json-project-json'; |
2 | 7 | import { createTaskGraph } from './create-task-graph';
|
3 | 8 |
|
4 | 9 | describe('createTaskGraph', () => {
|
@@ -1677,4 +1682,311 @@ describe('createTaskGraph', () => {
|
1677 | 1682 | 'lib3:build',
|
1678 | 1683 | ]);
|
1679 | 1684 | });
|
| 1685 | + |
| 1686 | + it('should handle multiple dependsOn task groups', () => { |
| 1687 | + const taskGraph = createTaskGraph( |
| 1688 | + { |
| 1689 | + nodes: { |
| 1690 | + a: { |
| 1691 | + name: 'a', |
| 1692 | + type: 'app', |
| 1693 | + data: { |
| 1694 | + root: 'a-root', |
| 1695 | + targets: { |
| 1696 | + deploy: { |
| 1697 | + executor: 'nx:run-commands', |
| 1698 | + dependsOn: [{ target: 'build' }], |
| 1699 | + }, |
| 1700 | + build: { |
| 1701 | + executor: 'nx:run-commands', |
| 1702 | + dependsOn: [{ target: 'compile' }], |
| 1703 | + }, |
| 1704 | + compile: { |
| 1705 | + executor: 'nx:run-commands', |
| 1706 | + dependsOn: ['^compile'], |
| 1707 | + }, |
| 1708 | + }, |
| 1709 | + }, |
| 1710 | + }, |
| 1711 | + b: { |
| 1712 | + name: 'b', |
| 1713 | + type: 'lib', |
| 1714 | + data: { |
| 1715 | + root: 'b-root', |
| 1716 | + targets: { |
| 1717 | + deploy: { |
| 1718 | + executor: 'nx:run-commands', |
| 1719 | + dependsOn: [{ target: 'build' }], |
| 1720 | + }, |
| 1721 | + build: { |
| 1722 | + executor: 'nx:run-commands', |
| 1723 | + dependsOn: [{ target: 'compile' }], |
| 1724 | + }, |
| 1725 | + compile: { |
| 1726 | + executor: 'nx:run-commands', |
| 1727 | + dependsOn: ['^compile'], |
| 1728 | + }, |
| 1729 | + }, |
| 1730 | + }, |
| 1731 | + }, |
| 1732 | + c: { |
| 1733 | + name: 'c', |
| 1734 | + type: 'lib', |
| 1735 | + data: { |
| 1736 | + root: 'c-root', |
| 1737 | + targets: { |
| 1738 | + deploy: { |
| 1739 | + executor: 'nx:run-commands', |
| 1740 | + dependsOn: [{ target: 'build' }], |
| 1741 | + }, |
| 1742 | + build: { |
| 1743 | + executor: 'nx:run-commands', |
| 1744 | + dependsOn: [{ target: 'compile' }], |
| 1745 | + }, |
| 1746 | + compile: { |
| 1747 | + executor: 'nx:run-commands', |
| 1748 | + dependsOn: ['^compile'], |
| 1749 | + }, |
| 1750 | + }, |
| 1751 | + }, |
| 1752 | + }, |
| 1753 | + d: { |
| 1754 | + name: 'd', |
| 1755 | + type: 'lib', |
| 1756 | + data: { |
| 1757 | + root: 'd-root', |
| 1758 | + targets: { |
| 1759 | + deploy: { |
| 1760 | + executor: 'nx:run-commands', |
| 1761 | + dependsOn: [{ target: 'build' }], |
| 1762 | + }, |
| 1763 | + build: { |
| 1764 | + executor: 'nx:run-commands', |
| 1765 | + dependsOn: [{ target: 'compile' }], |
| 1766 | + }, |
| 1767 | + compile: { |
| 1768 | + executor: 'nx:run-commands', |
| 1769 | + dependsOn: ['^compile'], |
| 1770 | + }, |
| 1771 | + }, |
| 1772 | + }, |
| 1773 | + }, |
| 1774 | + }, |
| 1775 | + dependencies: { |
| 1776 | + a: [], |
| 1777 | + b: [ |
| 1778 | + { |
| 1779 | + source: 'b', |
| 1780 | + target: 'd', |
| 1781 | + type: 'static', |
| 1782 | + }, |
| 1783 | + ], |
| 1784 | + c: [ |
| 1785 | + { |
| 1786 | + source: 'c', |
| 1787 | + target: 'd', |
| 1788 | + type: 'static', |
| 1789 | + }, |
| 1790 | + ], |
| 1791 | + d: [], |
| 1792 | + }, |
| 1793 | + }, |
| 1794 | + {}, |
| 1795 | + ['a', 'b'], |
| 1796 | + ['deploy'], |
| 1797 | + null, |
| 1798 | + {} |
| 1799 | + ); |
| 1800 | + |
| 1801 | + expect(taskGraph.dependencies['a:deploy']).toEqual(['a:build']); |
| 1802 | + expect(taskGraph.dependencies['a:build']).toEqual(['a:compile']); |
| 1803 | + expect(taskGraph.dependencies['a:compile']).toEqual([]); |
| 1804 | + expect(taskGraph.dependencies['b:deploy']).toEqual(['b:build']); |
| 1805 | + expect(taskGraph.dependencies['b:build']).toEqual(['b:compile']); |
| 1806 | + expect(taskGraph.dependencies['b:compile']).toEqual(['d:compile']); |
| 1807 | + expect(taskGraph.dependencies['d:compile']).toEqual([]); |
| 1808 | + }); |
| 1809 | + |
| 1810 | + it('should handle deep dependsOn groups', () => { |
| 1811 | + const taskGraph = createTaskGraph( |
| 1812 | + new GraphBuilder() |
| 1813 | + .addProjectConfiguration({ |
| 1814 | + name: 'app-1', |
| 1815 | + targets: { |
| 1816 | + deploy: { |
| 1817 | + executor: 'foo', |
| 1818 | + dependsOn: ['build'], |
| 1819 | + }, |
| 1820 | + build: { |
| 1821 | + executor: 'foo', |
| 1822 | + dependsOn: ['^build', 'codegen'], |
| 1823 | + }, |
| 1824 | + codegen: { |
| 1825 | + executor: 'foo', |
| 1826 | + }, |
| 1827 | + }, |
| 1828 | + }) |
| 1829 | + .addProjectConfiguration({ |
| 1830 | + name: 'app-2', |
| 1831 | + targets: { |
| 1832 | + deploy: { |
| 1833 | + executor: 'foo', |
| 1834 | + dependsOn: ['build'], |
| 1835 | + }, |
| 1836 | + build: { |
| 1837 | + executor: 'foo', |
| 1838 | + dependsOn: [ |
| 1839 | + '^build', |
| 1840 | + { |
| 1841 | + target: 'codegen', |
| 1842 | + params: 'forward', |
| 1843 | + }, |
| 1844 | + ], |
| 1845 | + }, |
| 1846 | + codegen: { |
| 1847 | + executor: 'foo', |
| 1848 | + }, |
| 1849 | + }, |
| 1850 | + }) |
| 1851 | + .addProjectConfiguration({ |
| 1852 | + name: 'app-3', |
| 1853 | + targets: { |
| 1854 | + deploy: { |
| 1855 | + executor: 'foo', |
| 1856 | + dependsOn: ['build'], |
| 1857 | + }, |
| 1858 | + build: { |
| 1859 | + executor: 'foo', |
| 1860 | + dependsOn: [ |
| 1861 | + '^build', |
| 1862 | + { |
| 1863 | + target: 'codegen', |
| 1864 | + params: 'forward', |
| 1865 | + }, |
| 1866 | + ], |
| 1867 | + }, |
| 1868 | + codegen: { |
| 1869 | + executor: 'foo', |
| 1870 | + }, |
| 1871 | + }, |
| 1872 | + }) |
| 1873 | + .addProjectConfiguration({ |
| 1874 | + name: 'lib-1', |
| 1875 | + targets: { |
| 1876 | + build: { |
| 1877 | + executor: 'foo', |
| 1878 | + dependsOn: ['^build', 'codegen'], |
| 1879 | + }, |
| 1880 | + codegen: { |
| 1881 | + executor: 'foo', |
| 1882 | + }, |
| 1883 | + }, |
| 1884 | + }) |
| 1885 | + .addProjectConfiguration({ |
| 1886 | + name: 'lib-2', |
| 1887 | + targets: { |
| 1888 | + build: { |
| 1889 | + executor: 'foo', |
| 1890 | + dependsOn: ['^build', 'codegen'], |
| 1891 | + }, |
| 1892 | + codegen: { |
| 1893 | + executor: 'foo', |
| 1894 | + }, |
| 1895 | + }, |
| 1896 | + }) |
| 1897 | + .addDependencies({ |
| 1898 | + 'app-1': ['lib-1'], |
| 1899 | + 'app-2': ['lib-2'], |
| 1900 | + 'app-3': ['lib-2'], |
| 1901 | + 'lib-1': ['lib-2'], |
| 1902 | + 'lib-2': [], |
| 1903 | + }) |
| 1904 | + .build(), |
| 1905 | + {}, |
| 1906 | + ['app-1', 'app-2', 'app-3'], |
| 1907 | + ['deploy', 'test'], |
| 1908 | + null, |
| 1909 | + {}, |
| 1910 | + false |
| 1911 | + ); |
| 1912 | + |
| 1913 | + expect(taskGraph.dependencies).toMatchInlineSnapshot(` |
| 1914 | + { |
| 1915 | + "app-1:build": [ |
| 1916 | + "lib-1:build", |
| 1917 | + "app-1:codegen", |
| 1918 | + ], |
| 1919 | + "app-1:codegen": [], |
| 1920 | + "app-1:deploy": [ |
| 1921 | + "app-1:build", |
| 1922 | + ], |
| 1923 | + "app-2:build": [ |
| 1924 | + "lib-2:build", |
| 1925 | + "app-2:codegen", |
| 1926 | + ], |
| 1927 | + "app-2:codegen": [], |
| 1928 | + "app-2:deploy": [ |
| 1929 | + "app-2:build", |
| 1930 | + ], |
| 1931 | + "app-3:build": [ |
| 1932 | + "lib-2:build", |
| 1933 | + "app-3:codegen", |
| 1934 | + ], |
| 1935 | + "app-3:codegen": [], |
| 1936 | + "app-3:deploy": [ |
| 1937 | + "app-3:build", |
| 1938 | + ], |
| 1939 | + "lib-1:build": [ |
| 1940 | + "lib-2:build", |
| 1941 | + "lib-1:codegen", |
| 1942 | + ], |
| 1943 | + "lib-1:codegen": [], |
| 1944 | + "lib-2:build": [ |
| 1945 | + "lib-2:codegen", |
| 1946 | + ], |
| 1947 | + "lib-2:codegen": [], |
| 1948 | + } |
| 1949 | + `); |
| 1950 | + }); |
1680 | 1951 | });
|
| 1952 | + |
| 1953 | +class GraphBuilder { |
| 1954 | + nodes: Record<string, ProjectGraphProjectNode> = {}; |
| 1955 | + deps: Record<string, string[]> = {}; |
| 1956 | + |
| 1957 | + addProjectConfiguration( |
| 1958 | + project: Omit<ProjectConfiguration, 'root'>, |
| 1959 | + type?: ProjectGraph['nodes'][string]['type'] |
| 1960 | + ) { |
| 1961 | + const t = type ?? 'lib'; |
| 1962 | + this.nodes[project.name] = { |
| 1963 | + name: project.name, |
| 1964 | + type: t, |
| 1965 | + data: { ...project, root: `${t}/${project.name}` }, |
| 1966 | + }; |
| 1967 | + return this; |
| 1968 | + } |
| 1969 | + |
| 1970 | + addDependencies(deps: Record<string, string[]>) { |
| 1971 | + for (const source of Object.keys(deps)) { |
| 1972 | + if (!this.deps[source]) { |
| 1973 | + this.deps[source] = []; |
| 1974 | + } |
| 1975 | + this.deps[source].push(...deps[source]); |
| 1976 | + } |
| 1977 | + return this; |
| 1978 | + } |
| 1979 | + |
| 1980 | + build(): ProjectGraph { |
| 1981 | + return { |
| 1982 | + nodes: this.nodes, |
| 1983 | + dependencies: Object.fromEntries( |
| 1984 | + Object.entries(this.deps).map(([k, v]) => [ |
| 1985 | + k, |
| 1986 | + v.map((d) => ({ source: k, target: d, type: 'static' })), |
| 1987 | + ]) |
| 1988 | + ), |
| 1989 | + externalNodes: {}, |
| 1990 | + }; |
| 1991 | + } |
| 1992 | +} |
0 commit comments