From 71a6de4947fd6f9cec9ad0e7c521cedc08f1cee4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= <bjorn@dahlgren.at>
Date: Sat, 30 May 2020 18:18:56 +0200
Subject: [PATCH] feat(cocoapods): use renovate cache (#6384)

---
 .../cocoapods/__snapshots__/artifacts.spec.ts.snap | 14 ++++++++++----
 lib/manager/cocoapods/artifacts.spec.ts            |  3 +++
 lib/manager/cocoapods/artifacts.ts                 |  4 ++++
 lib/manager/cocoapods/utils.ts                     | 14 ++++++++++++++
 4 files changed, 31 insertions(+), 4 deletions(-)
 create mode 100644 lib/manager/cocoapods/utils.ts

diff --git a/lib/manager/cocoapods/__snapshots__/artifacts.spec.ts.snap b/lib/manager/cocoapods/__snapshots__/artifacts.spec.ts.snap
index 4e81d1f5bb..4e09680e75 100644
--- a/lib/manager/cocoapods/__snapshots__/artifacts.spec.ts.snap
+++ b/lib/manager/cocoapods/__snapshots__/artifacts.spec.ts.snap
@@ -34,11 +34,12 @@ Array [
     },
   },
   Object {
-    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child --user=ubuntu -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/cocoapods:1.2.4 bash -l -c \\"pod install\\"",
+    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child --user=ubuntu -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/cache\\":\\"/tmp/cache\\" -e CP_HOME_DIR -w \\"/tmp/github/some/repo\\" renovate/cocoapods:1.2.4 bash -l -c \\"pod install\\"",
     "options": Object {
       "cwd": "/tmp/github/some/repo",
       "encoding": "utf-8",
       "env": Object {
+        "CP_HOME_DIR": "/tmp/cache/others/cocoapods",
         "HOME": "/home/user",
         "HTTPS_PROXY": "https://example.com",
         "HTTP_PROXY": "http://example.com",
@@ -74,11 +75,12 @@ Array [
     },
   },
   Object {
-    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child --user=ubuntu -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/cocoapods:latest bash -l -c \\"pod install\\"",
+    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child --user=ubuntu -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/cache\\":\\"/tmp/cache\\" -e CP_HOME_DIR -w \\"/tmp/github/some/repo\\" renovate/cocoapods:latest bash -l -c \\"pod install\\"",
     "options": Object {
       "cwd": "/tmp/github/some/repo",
       "encoding": "utf-8",
       "env": Object {
+        "CP_HOME_DIR": "/tmp/cache/others/cocoapods",
         "HOME": "/home/user",
         "HTTPS_PROXY": "https://example.com",
         "HTTP_PROXY": "http://example.com",
@@ -107,6 +109,7 @@ Array [
       "cwd": "/tmp/github/some/repo",
       "encoding": "utf-8",
       "env": Object {
+        "CP_HOME_DIR": "/tmp/cache/others/cocoapods",
         "HOME": "/home/user",
         "HTTPS_PROXY": "https://example.com",
         "HTTP_PROXY": "http://example.com",
@@ -142,6 +145,7 @@ Array [
       "cwd": "/tmp/github/some/repo",
       "encoding": "utf-8",
       "env": Object {
+        "CP_HOME_DIR": "/tmp/cache/others/cocoapods",
         "HOME": "/home/user",
         "HTTPS_PROXY": "https://example.com",
         "HTTP_PROXY": "http://example.com",
@@ -188,11 +192,12 @@ Array [
     },
   },
   Object {
-    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/cocoapods bash -l -c \\"gem install cocoapods-acknowledgements && pod install\\"",
+    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/cache\\":\\"/tmp/cache\\" -e CP_HOME_DIR -w \\"/tmp/github/some/repo\\" renovate/cocoapods bash -l -c \\"gem install cocoapods-acknowledgements && pod install\\"",
     "options": Object {
       "cwd": "/tmp/github/some/repo",
       "encoding": "utf-8",
       "env": Object {
+        "CP_HOME_DIR": "/tmp/cache/others/cocoapods",
         "HOME": "/home/user",
         "HTTPS_PROXY": "https://example.com",
         "HTTP_PROXY": "http://example.com",
@@ -251,11 +256,12 @@ Array [
     },
   },
   Object {
-    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/cocoapods bash -l -c \\"pod install\\"",
+    "cmd": "docker run --rm --name=renovate_cocoapods --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/cache\\":\\"/tmp/cache\\" -e CP_HOME_DIR -w \\"/tmp/github/some/repo\\" renovate/cocoapods bash -l -c \\"pod install\\"",
     "options": Object {
       "cwd": "/tmp/github/some/repo",
       "encoding": "utf-8",
       "env": Object {
+        "CP_HOME_DIR": "/tmp/cache/others/cocoapods",
         "HOME": "/home/user",
         "HTTPS_PROXY": "https://example.com",
         "HTTP_PROXY": "http://example.com",
diff --git a/lib/manager/cocoapods/artifacts.spec.ts b/lib/manager/cocoapods/artifacts.spec.ts
index 24156f3aaf..97571ffd70 100644
--- a/lib/manager/cocoapods/artifacts.spec.ts
+++ b/lib/manager/cocoapods/artifacts.spec.ts
@@ -23,8 +23,11 @@ const env = mocked(_env);
 const platform = mocked(_platform);
 const datasource = mocked(_datasource);
 
+delete process.env.CP_HOME_DIR;
+
 const config = {
   localDir: join('/tmp/github/some/repo'),
+  cacheDir: join('/tmp/cache'),
 };
 
 describe('.updateArtifacts()', () => {
diff --git a/lib/manager/cocoapods/artifacts.ts b/lib/manager/cocoapods/artifacts.ts
index 7e7d291b4d..2436c02f38 100644
--- a/lib/manager/cocoapods/artifacts.ts
+++ b/lib/manager/cocoapods/artifacts.ts
@@ -9,6 +9,7 @@ import {
   writeLocalFile,
 } from '../../util/fs';
 import { UpdateArtifact, UpdateArtifactsResult } from '../common';
+import { getCocoaPodsHome } from './utils';
 
 const pluginRegex = /^\s*plugin\s*(['"])(?<plugin>[^'"]+)\1/;
 
@@ -69,6 +70,9 @@ export async function updateArtifacts({
   const cmd = [...getPluginCommands(newPackageFileContent), 'pod install'];
   const execOptions: ExecOptions = {
     cwdFile: packageFileName,
+    extraEnv: {
+      CP_HOME_DIR: await getCocoaPodsHome(config),
+    },
     docker: {
       image: 'renovate/cocoapods',
       tagScheme: 'ruby',
diff --git a/lib/manager/cocoapods/utils.ts b/lib/manager/cocoapods/utils.ts
new file mode 100644
index 0000000000..cf2383a978
--- /dev/null
+++ b/lib/manager/cocoapods/utils.ts
@@ -0,0 +1,14 @@
+import { join } from 'upath';
+import { logger } from '../../logger';
+import { ensureDir } from '../../util/fs';
+import { UpdateArtifactsConfig } from '../common';
+
+export async function getCocoaPodsHome(
+  config: UpdateArtifactsConfig
+): Promise<string> {
+  const cacheDir =
+    process.env.CP_HOME_DIR || join(config.cacheDir, './others/cocoapods');
+  await ensureDir(cacheDir);
+  logger.debug(`Using cocoapods home ${cacheDir}`);
+  return cacheDir;
+}
-- 
GitLab